设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7550|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;, m- F3 d# Y  l/ [% T+ @
import java.io.BufferedReader;
7 V7 p. @/ U' S. ?4 J+ U3 `1 s5 j3 [import java.io.FileInputStream;) ]+ N: E5 y* l" B
import java.io.FileNotFoundException;3 A: v' y- A1 T) H8 i2 P4 P
import java.io.IOException;/ o. s. X) M7 T" w: c- @# W
import java.io.InputStreamReader;  t# u6 o9 U" g# H
import java.io.UnsupportedEncodingException;
+ W/ z5 _* q- R# h4 l7 V: ?7 Gimport java.util.StringTokenizer;, c( E- p# [; F! L5 ~( ]& P6 S
public class TXTReader {
8 F, J1 O7 s4 V9 O, ] protected String matrix[][];
# W9 d2 R9 y  ^& N$ Z+ ? protected int xSize;
' l  `8 g. g+ O# F& t4 O* o8 K protected int ySize;5 t, O/ ?" L  D7 P  H: h; l7 L
public TXTReader(String sugarFile) {7 F  `9 T( Z2 Q
  java.io.InputStream stream = null;
& [! G5 V- ^5 T4 r0 |1 s# C+ i, V  try {) o: J7 h, b4 Y" P1 d7 u: {
   stream = new FileInputStream(sugarFile);
  s4 l9 i: i* }) {  } catch (FileNotFoundException e) {# T9 |3 A$ O1 ^
   e.printStackTrace();+ T7 u- \* m" ^: v
  }
: n7 [; t6 q2 q- X+ S6 w3 B  BufferedReader in = new BufferedReader(new InputStreamReader(stream));. G* b/ J, U+ Y
  init(in);- K2 m2 O  d1 J
}% g7 U) b$ l2 h. C0 g
private void init(BufferedReader in) {& o- o7 }1 c9 [# T  c1 v
  try {6 R# T' x, P) P. f+ @2 |
   String str = in.readLine();! j# G1 O" {3 l% f, f" j
   if (!str.equals("b2")) {6 i$ N5 d! J7 }3 ~+ F1 X
    throw new UnsupportedEncodingException(
+ K3 v! J( m% s: q4 X      "File is not in TXT ascii format");
  h; t% @; |. @, J) z$ Q   }
+ R* J: S( O3 h% ~   str = in.readLine();
5 }. s9 t/ W5 ?# s   String tem[] = str.split("[\\t\\s]+");
: |( z. ~! E$ [# h3 }. x5 a" G4 p   xSize = Integer.valueOf(tem[0]).intValue();! A. p0 f: K  F
   ySize = Integer.valueOf(tem[1]).intValue();
7 Y5 c7 L2 @, q2 ~" c+ i   matrix = new String[xSize][ySize];' c% V/ W- r& D; e$ O" |
   int i = 0;( z, ]" x8 C4 g
   str = "";& }9 D1 Z' ^2 W% o% K$ R7 x* U
   String line = in.readLine();
1 o; `) c5 o% M# Z- h- ?8 b1 r   while (line != null) {0 I1 q) v% b; p/ ?, n5 O! K. D
    String temp[] = line.split("[\\t\\s]+");$ v3 G- C; b# _7 q2 H# Y3 @; `
    line = in.readLine();
7 _* F5 O# O  F/ a) s0 H    for (int j = 0; j < ySize; j++) {: ~2 A/ W2 _  s+ |; t4 L3 W
     matrix[i][j] = temp[j];7 S1 y) `5 `8 q- ]
    }
' C( r4 [# r7 `! N# X    i++;) P( e( f: N4 X+ F. L
   }
! h: B4 l- ~# @7 I- B/ l   in.close();$ m8 c: W% T+ Y% h0 O9 k
  } catch (IOException ex) {% y+ Y; f) A6 v# e. L
   System.out.println("Error Reading file");
  |0 J9 s) Y8 _7 e   ex.printStackTrace();
9 M  Z( E) X! u5 U   System.exit(0);3 n+ a# A8 B( M1 ?
  }4 p8 Q6 Z( g" t# F
}' S! K6 E/ e/ B1 ~( m( j
public String[][] getMatrix() {" w9 i* J  Q% `" W5 r
  return matrix;. `* I  b$ j! K+ \: E0 _
}
# m% P( P9 B7 V) m1 J}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-26 22:53 , Processed in 0.020087 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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