设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9516|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;) i5 W. d7 s" \& r$ c3 ?
import java.io.BufferedReader;
1 i4 O) A7 Y  n: nimport java.io.FileInputStream;8 j+ ]% L6 k( L% s, ?0 |1 q
import java.io.FileNotFoundException;
5 {& C- o! s8 ~) q$ Himport java.io.IOException;
5 d5 \$ B) I' u/ s( eimport java.io.InputStreamReader;1 ^. p* `4 f# ^
import java.io.UnsupportedEncodingException;0 M7 R6 |3 s( \5 d0 Z8 R8 ?
import java.util.StringTokenizer;
- J+ K" v1 O8 N0 U/ Spublic class TXTReader {
+ v9 Y, U7 F" {7 [& e protected String matrix[][];
! u$ Z' {" J8 u0 u8 m) A6 @3 I7 J protected int xSize;
. _0 }: L, x, `( s& _ protected int ySize;/ |  R& _' [. Z5 t, }& z; A3 a* ^
public TXTReader(String sugarFile) {) \% v6 I& U* F- j% a( R3 e9 N" }
  java.io.InputStream stream = null;& Q0 f8 q" Y3 r2 H! O
  try {5 a6 ]2 ?+ v, K: h7 \. [$ s
   stream = new FileInputStream(sugarFile);6 v6 H) E; v, M* T
  } catch (FileNotFoundException e) {
/ ?8 @( A8 _" [* i2 u   e.printStackTrace();) Y. G1 g* z$ e1 p) |8 f  E
  }
. n" f+ x- [" E7 x  I5 t/ N) j/ m  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
+ Y5 k0 N- ^" B! C$ x9 ^' z  init(in);
0 }' H. g) z- f$ S) m/ `" ? }
' P5 I" G7 i8 M& `0 D5 P0 h0 q) q  j private void init(BufferedReader in) {
/ @6 m7 n2 R; z5 q& Y: l  try {
& R3 h* o8 j! O6 G* @   String str = in.readLine();, p2 l" x' m: Z
   if (!str.equals("b2")) {
  H0 J' k+ p; N4 J    throw new UnsupportedEncodingException(- _/ U3 H5 o1 e# Z$ S- G! _
      "File is not in TXT ascii format");
  x) l7 q8 [) x   }
+ P7 A" }: n) \' h/ x   str = in.readLine();  b7 ?# n# D- q/ o$ ?# e
   String tem[] = str.split("[\\t\\s]+");
) J6 b& J" D/ l- I2 L   xSize = Integer.valueOf(tem[0]).intValue();5 g) Y+ S) P- x) e6 {2 c+ {: D3 o$ [
   ySize = Integer.valueOf(tem[1]).intValue();
- ?' m7 o; O: g8 ]   matrix = new String[xSize][ySize];
; W" r; {3 X& e1 D   int i = 0;" @; s5 K( `, r- A5 l
   str = "";
2 T6 r" K* x9 t% j7 e8 o# ^   String line = in.readLine();2 x# ^5 m# N7 `( f/ f
   while (line != null) {
% R% Y# u6 @3 X    String temp[] = line.split("[\\t\\s]+");
% [2 v. X0 s5 n( y2 p    line = in.readLine();
" u8 K0 K' J8 u  j6 h2 p    for (int j = 0; j < ySize; j++) {2 g$ P( ?& u# o  w, B1 y
     matrix[i][j] = temp[j];# X) Q- T! O; ~( M, }: {7 ]& l
    }
* f+ g) J7 e" l; \7 Q' Z    i++;& {+ c5 }7 A) f# u5 ?% m
   }+ H6 h* X$ E4 Z& x# Y
   in.close();
" T- V1 e5 A% ~  } catch (IOException ex) {9 N' k9 |$ Z5 A1 K* r
   System.out.println("Error Reading file");
. ]' ^. s9 Z% }' P) \8 F. n   ex.printStackTrace();% e6 L/ _: y8 G9 ~2 a+ d
   System.exit(0);& M6 Q4 i. ^5 r& |" q% C# O
  }
/ y: F* z1 M5 ]  s' w* M; a }
4 o  l" y% @* ^* ]3 w* h* Z  g public String[][] getMatrix() {
% O$ Z* m5 |) Z8 w  return matrix;- k. e& l: Z& e3 e  L4 x. J% ^
}
0 E, x( @; P! `: c}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-22 18:36 , Processed in 0.015653 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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