设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7777|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;3 {8 U. V, ~7 u& b8 [& u2 M& f5 F
import java.io.BufferedReader;
1 {: {- Z7 s) N$ E* @import java.io.FileInputStream;( {6 ]7 ~% i" A+ g0 X  j* y' t% R3 ]
import java.io.FileNotFoundException;
9 M" V. C# i2 X* uimport java.io.IOException;
; }; _2 A  s7 b( ]: vimport java.io.InputStreamReader;% Z* q$ X7 s; y* E- Y
import java.io.UnsupportedEncodingException;
. c6 s$ Y, k1 e# I5 r# A& ]import java.util.StringTokenizer;. u; o: h8 s: E& f4 F" s$ L
public class TXTReader {" ]! u9 J( B( d6 y; V
protected String matrix[][];+ z4 n8 \" g/ n: b
protected int xSize;; o- p5 S9 g+ h* G( Q5 i
protected int ySize;
& G8 l' U4 D: H* a* s: @" K1 f! } public TXTReader(String sugarFile) {
8 d1 `8 K: A- d* T) F$ X  java.io.InputStream stream = null;) I0 N& _; R* C
  try {/ Y2 H/ T! S8 f6 s& q+ i/ O
   stream = new FileInputStream(sugarFile);2 J% k6 W( F- }# R
  } catch (FileNotFoundException e) {. t) h. ^5 m4 A
   e.printStackTrace();4 \4 b+ S9 O* k$ f' Z  C7 U
  }
$ T1 O% H# Y2 E5 h  BufferedReader in = new BufferedReader(new InputStreamReader(stream));9 K4 w6 N% b6 {# n6 H
  init(in);7 W% i: q+ ]6 z/ K* b" j
}7 r* O" F7 {5 K, s* R7 |
private void init(BufferedReader in) {) c+ N6 `. N: ~& ~
  try {
% x- ?0 Q4 \4 W  e/ K  f, \   String str = in.readLine();
, J8 t# [; m, R   if (!str.equals("b2")) {
* B; y* Z; W; A" V! @    throw new UnsupportedEncodingException(5 E0 @4 l/ ~& c# O8 {! n
      "File is not in TXT ascii format");
1 r0 S* h/ k5 Z   }/ `# x4 b/ f1 P& ], W" R5 N3 y
   str = in.readLine();7 \9 s, g/ g, D: ~. R# N7 i
   String tem[] = str.split("[\\t\\s]+");& e6 ]3 [4 h  k; b; `1 l) V
   xSize = Integer.valueOf(tem[0]).intValue();2 e' `7 }, z+ v& e% r! e
   ySize = Integer.valueOf(tem[1]).intValue();
5 s8 R- |4 `4 ]& U   matrix = new String[xSize][ySize];
8 t+ p1 D2 B: e2 _   int i = 0;9 h' r( o8 Q" h5 ^! j+ T5 N' k1 C- H
   str = "";% X7 \0 H- h6 i; I% F# p: s
   String line = in.readLine();7 }1 {9 J- g1 h/ C7 N$ }% k0 a) M( V
   while (line != null) {
6 C% j' S3 p3 `/ o& e, ^    String temp[] = line.split("[\\t\\s]+");
8 P* o3 y; I, {    line = in.readLine();
0 T* Y  n; c9 Y0 B1 \    for (int j = 0; j < ySize; j++) {6 M6 {% U7 P/ E3 Q# }0 C
     matrix[i][j] = temp[j];/ t% ~; O4 F5 M5 I* j' t  b* ^
    }( u  }' j& n  y6 [5 S: R4 V
    i++;4 \4 t6 K0 p4 ~- d
   }. _/ A- f+ i" N) M8 J
   in.close();
+ m7 z' u" G1 C+ G  } catch (IOException ex) {
9 i9 T* [% U& d4 d- ?; }   System.out.println("Error Reading file");8 }/ T1 z7 R! x2 [6 V( H
   ex.printStackTrace();+ ~; `' p8 X3 i5 d8 D$ b; M
   System.exit(0);4 C6 ?2 F0 p, h- N& y, E
  }
7 o! R+ N( }8 @' H }
& N4 C( {  }. \* X/ }' D+ T1 j public String[][] getMatrix() {
( G4 I7 w" D# P1 ?5 ~* S0 Z) ^  return matrix;7 N5 U# G$ _: w
}. [8 Y4 S( |( \4 T1 S4 e1 |
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-13 13:22 , Processed in 0.015323 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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