设为首页收藏本站

最大的系统仿真与系统优化公益交流社区

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7668|回复: 1

[原创] 发一个读取TXT文件数据的程序代码

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
; q9 k7 j# p) D, A* M% Y7 H0 [* _- X4 Vimport java.io.BufferedReader;; }2 `) R1 A: p0 d
import java.io.FileInputStream;  F1 n7 ~1 f! _* Y4 U! }
import java.io.FileNotFoundException;3 U/ V4 o) q( v
import java.io.IOException;- U  |, t8 u5 J7 ^6 M5 D7 B$ T2 M
import java.io.InputStreamReader;
8 e( M2 r% D/ @8 E% Nimport java.io.UnsupportedEncodingException;
  p2 v6 }5 b3 d* N, N, bimport java.util.StringTokenizer;! ]# t- m5 {4 W; p
public class TXTReader {
2 \* M" f& f! n# n* A7 g' }, h protected String matrix[][];
0 \* n% h! y) ^! F) R protected int xSize;
* i. g; {6 {/ d protected int ySize;' E) T8 X$ b* J6 A
public TXTReader(String sugarFile) {
5 j( l9 s  o6 U  java.io.InputStream stream = null;
  F" _1 o/ D) W# _( Q9 F4 Q  try {
: b( R% O5 S" Z+ i# x7 K8 J% V   stream = new FileInputStream(sugarFile);
0 F$ k( E; d8 L4 S  } catch (FileNotFoundException e) {! y# \2 O4 j- F# K$ Z
   e.printStackTrace();* G9 m0 N8 j% I$ V
  }3 {) ?2 z( Y$ Y- L, D+ S
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
' W+ i& d$ y% X6 U' _3 p  init(in);
) u' o$ N3 P$ _# U }% C) [8 Z; o  o5 |( K5 H
private void init(BufferedReader in) {
+ ~1 V! S4 F& k% L- t1 Z  try {7 q0 v! E# W4 P. A4 i  J
   String str = in.readLine();+ i" V1 A1 \. A( |+ I& n
   if (!str.equals("b2")) {
* Y' ^4 y; V& k# `  C* ]    throw new UnsupportedEncodingException(
8 b# ?1 F+ V2 _' B/ A  m5 r      "File is not in TXT ascii format");
: D; e: h  q! E2 J9 I/ o   }
) _8 F+ G& P9 Q   str = in.readLine();+ U/ c: T7 t# O$ P3 z% k% U- h
   String tem[] = str.split("[\\t\\s]+");
- a* |( y  U8 }. U. W8 O   xSize = Integer.valueOf(tem[0]).intValue();
" p9 l! v8 S/ p' u5 a; X- @+ g   ySize = Integer.valueOf(tem[1]).intValue();& N' Q8 O4 B  {9 x8 G5 `
   matrix = new String[xSize][ySize];6 {9 {1 P. m- T9 y3 i. g1 S( I, H) i
   int i = 0;
8 y9 s7 p$ a3 Z" w3 Y, w* _4 J   str = "";
- ?, q* S  U; I4 Q% k: j   String line = in.readLine();
+ i4 n) w- I0 g   while (line != null) {7 ]# i0 _6 h5 p/ V- L$ W
    String temp[] = line.split("[\\t\\s]+");. a% z+ j8 b1 b( r1 t
    line = in.readLine();$ n; ^/ `' w& L2 T8 ^) ]2 E
    for (int j = 0; j < ySize; j++) {
3 I/ t- g! I! Z1 q% a     matrix[i][j] = temp[j];
* Y' y6 F: i% v: X& ?% D    }
4 ]; c" p8 P( i- L+ r    i++;
" O4 V1 m  ]: ?   }
! x: z! k9 H/ t. J" ?$ Z/ u   in.close();
/ G2 J3 s: d  }2 P! f, n  } catch (IOException ex) {
8 W0 H: j; w- x7 q& ^   System.out.println("Error Reading file");* T! Y) _8 Q4 ~/ B8 F
   ex.printStackTrace();
7 K6 y2 O# ?% D; G6 q   System.exit(0);
: ?. x8 Z$ ~3 {8 P  }
# [* e' R* a. P" q7 b" Q }; W4 y. A" J8 U- R2 {
public String[][] getMatrix() {
/ D9 w$ V1 M3 {4 D# S  return matrix;
6 \- b! ~7 Q, w" Y+ D4 o) A }
8 Q  w- ~9 |$ J) [}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|SimulWay 道于仿真   

GMT+8, 2026-4-6 00:35 , Processed in 0.019167 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表