设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8627|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;9 ^5 p6 e  I+ n) j  Q2 l: [) D' z; R
import java.io.BufferedReader;
: Z# ]4 c# a+ x7 Q$ d6 Yimport java.io.FileInputStream;
9 @7 _3 k% a+ U  O1 Nimport java.io.FileNotFoundException;$ _# f8 f% @5 q: }) o) K8 E
import java.io.IOException;
& `# `9 ^. I8 F5 r% }import java.io.InputStreamReader;9 G6 R+ z% l: {9 p
import java.io.UnsupportedEncodingException;2 O' u5 p1 v6 k3 L# e* ~% H
import java.util.StringTokenizer;
; g8 V; F6 T0 I0 T- Fpublic class TXTReader {2 ]+ G9 t+ J' i
protected String matrix[][];3 B( c3 q. T& E/ y
protected int xSize;1 r# P' |4 ^- _3 U# b& j
protected int ySize;
! g4 J) H6 J  C public TXTReader(String sugarFile) {* K# }* T/ F! d; O! `
  java.io.InputStream stream = null;
. j4 |$ J" @" E2 `/ W  try {
" n9 ~% n5 |8 ?+ ^& ^4 J( j7 {   stream = new FileInputStream(sugarFile);9 A- ~2 Q8 s0 M, ^
  } catch (FileNotFoundException e) {. J( {1 |: m2 b, K) r
   e.printStackTrace();
5 ^( v. L& o' L/ `  }, Y3 Y, ?! p6 I- o
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));3 R& @7 l: z2 S
  init(in);
" b8 x+ \2 c' c+ `# | }7 C3 T* Q; p5 s2 g
private void init(BufferedReader in) {; n+ {8 f, _) e9 z7 I( z
  try {, K7 o/ y( v* Z
   String str = in.readLine();
4 |, v% C3 y# d, f   if (!str.equals("b2")) {
+ m7 _, q! ~. g    throw new UnsupportedEncodingException(
0 W! C: L4 K# I* d' Z      "File is not in TXT ascii format");- M5 f$ t. m( J& D8 T  ^
   }
* M4 b5 Q9 U2 c, N6 w; l5 J   str = in.readLine();6 o  W/ i6 J! M. f/ ]- c) x3 @% o
   String tem[] = str.split("[\\t\\s]+");
' A9 J; `+ {: I$ ~! _) o7 C   xSize = Integer.valueOf(tem[0]).intValue();& ?7 {2 p" Q: Y8 j4 x  I* G0 F1 t
   ySize = Integer.valueOf(tem[1]).intValue();
7 D) `. V9 t1 M6 K: D   matrix = new String[xSize][ySize];
3 p1 n8 q% e+ `+ p+ F* u3 U- P   int i = 0;
9 ]& ?8 [; [* H- L( |& g( ^* v( h   str = "";3 S0 u: }: X  t, b# n
   String line = in.readLine();' A/ g4 F# H# m! ^$ R9 {* c
   while (line != null) {6 L5 V8 E' Y; j- U" D
    String temp[] = line.split("[\\t\\s]+");9 X7 m8 G; Q' p2 M
    line = in.readLine();4 @6 a0 M# b$ T' H: h
    for (int j = 0; j < ySize; j++) {
. k8 u# a! Y* _3 A     matrix[i][j] = temp[j];. w3 e7 ~1 [( l- W
    }5 d) t4 l6 d, }
    i++;3 c' H) g8 ?0 D( W
   }& a; I4 |# H% K# ?* I' I
   in.close();7 R" q+ l- ]3 G" I
  } catch (IOException ex) {9 Z/ P7 O6 r- H9 ^' E
   System.out.println("Error Reading file");& \# P) ~3 H% y- I& m% x0 j
   ex.printStackTrace();
+ ?8 b1 K$ n% `& B6 M5 ]- x   System.exit(0);5 i, `* \. u' |9 y1 c' i& y
  }# |/ z9 `6 l$ f
}
2 n( {) n1 C. O4 } public String[][] getMatrix() {
8 F+ T: G# c; ?% H, H  return matrix;
" ?9 r( ?% ]2 h9 w }
/ @2 u% q5 [' U' Q: @* R5 n/ D3 ^}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-4 08:39 , Processed in 0.013907 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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