设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9010|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
. ]& G& x2 }( ^% ^- p1 l! h& vimport java.io.BufferedReader;5 w$ Q$ v6 z/ ]5 y$ L5 V2 ^
import java.io.FileInputStream;
; X; [. x) P8 V0 H; {4 ?import java.io.FileNotFoundException;
" J( Z2 q: ?: d" `8 Q( jimport java.io.IOException;
& j  ^/ p' _- m" kimport java.io.InputStreamReader;
3 v* z- r2 j5 D3 D6 bimport java.io.UnsupportedEncodingException;
  y- K% t. j; C4 Kimport java.util.StringTokenizer;
1 d( Z- Y' o9 t  Y# Y+ n7 Y8 y* \public class TXTReader {
+ d+ K* J# P2 z$ e protected String matrix[][];, ~: I* M% W  |8 d+ m1 a  f
protected int xSize;
; B7 J2 U4 [% m) S protected int ySize;" y% u% ~0 b$ I! ?4 j
public TXTReader(String sugarFile) {
3 U7 T' l" E+ o, U; d3 T5 h  java.io.InputStream stream = null;  [2 h. P- _% g1 z. c+ k; p$ k
  try {
8 T5 F: `2 H3 @" @1 ?   stream = new FileInputStream(sugarFile);
9 a9 K, U# G) Y0 A) Z  } catch (FileNotFoundException e) {$ b" k7 k! P: R; l/ R2 _& y
   e.printStackTrace();8 l7 F9 I* v5 R5 F( D/ b# h
  }; p0 j0 R9 R. d4 F' u
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));, K( S* x' e0 x, n& B; R1 m" i
  init(in);
( n9 l- V2 e' f% K$ M }2 k$ z" @  Y  {! h3 t
private void init(BufferedReader in) {( _3 R$ q7 y8 @& B. n& R
  try {" i/ i- f5 z+ e6 G0 [
   String str = in.readLine();$ h( f. ?: }  B& N% @  v
   if (!str.equals("b2")) {2 }+ ?6 L1 l+ O1 c- M& T# R
    throw new UnsupportedEncodingException(
: L" X% a4 W4 |. k" W! C% \8 v      "File is not in TXT ascii format");
1 x' j4 R( O  R( K  ?* [   }( t5 @( Z- I% f
   str = in.readLine();6 r, W, [- V3 s% Q( @
   String tem[] = str.split("[\\t\\s]+");7 I7 I5 a0 P" U6 W# U' w
   xSize = Integer.valueOf(tem[0]).intValue();# G) A  ]" v3 O  n% f. i: C, Q
   ySize = Integer.valueOf(tem[1]).intValue();
9 ]" k' M0 i: v   matrix = new String[xSize][ySize];
+ O) H; P' S6 t+ U% j   int i = 0;
" f! n  b& e; o( j- L8 j  y   str = "";
: y+ Y5 P5 M6 B$ B. _: R   String line = in.readLine();# @1 R# j+ \& }  i' R; f  M1 Q4 W
   while (line != null) {/ k0 d5 u2 C& Y6 p
    String temp[] = line.split("[\\t\\s]+");! i4 F. A( i; O' @& v& m. y  U
    line = in.readLine();
# {3 ^0 V/ m. c8 D# ~    for (int j = 0; j < ySize; j++) {
. w& q& I6 R% Z     matrix[i][j] = temp[j];
3 ?( G4 U, Q2 g    }4 T) V5 f4 M7 v5 ]: e) i% R
    i++;
2 n3 d. i+ O1 H; n: R& w+ I5 @   }9 X7 B& x* _9 d0 J% ^# n. Y# ^2 C4 ^
   in.close();
9 z3 p9 b# P# Q4 d/ ?  } catch (IOException ex) {! t/ m: ~* i$ b
   System.out.println("Error Reading file");% x( e. q: E, F( F
   ex.printStackTrace();
2 h- r  G2 D* E# ^   System.exit(0);
- G$ V* o, Z! t8 v  }! s. I5 G; z, y% h
}
2 F5 a( u& r3 f' x  N3 S public String[][] getMatrix() {- _2 B4 S) Y5 e; |, S& Y; U
  return matrix;" `% p: t+ S0 m" `
}/ S  j+ r( _# n* ]7 X
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-26 09:22 , Processed in 0.015505 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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