设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6184|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
5 h9 D9 G" O. _/ Z" v& ?3 S& V. kimport java.io.BufferedReader;4 A5 ]8 d+ k  d) {% K
import java.io.FileInputStream;
( N' G; ?. F. b5 ~% S3 Rimport java.io.FileNotFoundException;! _5 p. L) X" Q
import java.io.IOException;  @" B( E+ G4 v; `
import java.io.InputStreamReader;% i4 V% \" n! x2 c1 t/ n
import java.io.UnsupportedEncodingException;3 n$ l# C- ~3 w5 t! |2 c1 ?
import java.util.StringTokenizer;. N; P) ]* F3 S+ J8 H) M
public class TXTReader {; m6 @9 |$ N* L8 p5 P2 V3 l
protected String matrix[][];
$ H9 p) K0 P% U& K. D7 ] protected int xSize;! w6 |; P# I' H
protected int ySize;0 e0 m. U9 F& L2 e7 j
public TXTReader(String sugarFile) {
) T# y) y3 j4 D* o# S# W  java.io.InputStream stream = null;$ p" P3 h  V& Y/ Z3 `
  try {/ W2 |- z* Z' ]# l
   stream = new FileInputStream(sugarFile);
: U" \& Z: K5 F) m# d6 z. a- N  } catch (FileNotFoundException e) {0 N# S2 X4 p# Y/ h0 h+ c
   e.printStackTrace();  e1 }  Z1 K( s
  }) K4 X8 T. r# z9 l4 ~7 Y1 O
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));$ |" o" ~" J6 W. }' y# J
  init(in);+ L0 k% y1 V4 F' i
}
- ^& f6 D! c* l! }/ ]8 R private void init(BufferedReader in) {. _; m- Q1 a, K
  try {
0 _0 L/ f2 W, j' L+ o. Q/ ^" X% \   String str = in.readLine();
/ @# {( x: `9 t6 S# c4 H2 J& ?   if (!str.equals("b2")) {
1 h9 e0 y6 ~8 Z# W6 }  a    throw new UnsupportedEncodingException(
, @2 e' a% i6 v0 [% g3 K; W9 T      "File is not in TXT ascii format");$ i! f' H" x1 i6 |2 e  [. q
   }
1 r$ G( p9 g" x9 |7 h   str = in.readLine();
% N5 E6 n4 N9 P$ H! q1 }7 |   String tem[] = str.split("[\\t\\s]+");& H% e5 |8 C8 S. L# `7 w
   xSize = Integer.valueOf(tem[0]).intValue();
6 l5 L* C( K" h$ V  G: m   ySize = Integer.valueOf(tem[1]).intValue();
1 z  n) j1 d5 n" h3 g8 \' |6 x   matrix = new String[xSize][ySize];' H. E/ s2 a5 S& _6 X
   int i = 0;
0 ^8 y; _4 G( e  O# w   str = "";
, z# b9 `, R) |# I8 @   String line = in.readLine();# ]$ B* B0 Y+ ?/ B3 Q4 j
   while (line != null) {
+ A- A1 Q# ?; `7 @: y0 p    String temp[] = line.split("[\\t\\s]+");1 P( }. p9 J& b( a
    line = in.readLine();
( y: k! p' j+ ~% r$ q& @    for (int j = 0; j < ySize; j++) {! h8 f6 i  l8 g- k* F. f
     matrix[i][j] = temp[j];
9 S' ^0 k' @& T* R! L: I5 j% H! U    }1 j# h" K4 P% V+ c; U! X3 E
    i++;! |& s9 z6 c) F- M5 |8 Z7 K$ c4 x8 n
   }
6 Q. a  S; D* v$ n   in.close();
/ R# Z, S8 d. {2 N  } catch (IOException ex) {
& k" e% Q0 V, r9 v; A   System.out.println("Error Reading file");
% H  ]- G" ~' n2 P0 C, z& Y   ex.printStackTrace();
* ]6 I6 J# r  v8 W7 O. q5 o   System.exit(0);% A# p4 B- y  _- r9 c+ ~+ W
  }
9 w+ H7 c. w3 j& X% Q6 a2 z% B }
  L0 C& w7 z/ ~# x- F# u/ U public String[][] getMatrix() {
1 F% V8 |$ x9 C0 b3 ?( G1 [  return matrix;& a; l9 T. n8 `: ^- y
}
, h  r, ^) A/ S}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-15 07:43 , Processed in 0.033477 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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