设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7880|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
. M- a3 V/ [# p2 p4 mimport java.io.BufferedReader;) s- H( H, h& `- A
import java.io.FileInputStream;
2 p; _, j! i1 ^' g0 N% Ximport java.io.FileNotFoundException;
1 B+ y. B% ~' A) O+ Jimport java.io.IOException;
  g. e. d: V8 _8 L" Jimport java.io.InputStreamReader;0 j6 O9 d) f, t) b! x/ V* L
import java.io.UnsupportedEncodingException;
* g. Q/ O1 ~' \% O( Gimport java.util.StringTokenizer;
0 n4 R' W$ r+ A3 p: ]- xpublic class TXTReader {$ j% J) s  }6 `; o# ], W! j
protected String matrix[][];( N2 M9 _/ ]" L# H  f& z. G
protected int xSize;
+ t- m/ X( `" ^1 ]- h) N% A. k protected int ySize;
* [) s8 Q  F! q9 d5 Z6 q; B; R public TXTReader(String sugarFile) {
' D4 \2 [9 b6 A( E7 j, U! s  java.io.InputStream stream = null;; R8 |, A( a  [; B; Q7 R
  try {
6 N- ~4 _3 H& q, N0 t4 W   stream = new FileInputStream(sugarFile);
& y( O  {1 a, s8 t+ J6 `  } catch (FileNotFoundException e) {
/ R* d2 Q3 m' P   e.printStackTrace();! H+ I7 S) n$ r( l+ J0 a
  }
$ J$ m6 O/ s" Y; ]. k  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
+ L2 L! e! s+ o- i, t$ h1 S1 w  init(in);
5 E7 a6 M' o) H% Y6 G' b }/ W' \5 u+ S6 G8 k3 t* B& }' y
private void init(BufferedReader in) {
8 P  C4 Y& ?+ U* U/ t7 Q  try {3 E, K8 p% w/ L# \' u, y2 i
   String str = in.readLine();
! i- }) U: |' V( S0 U) J2 z* A+ ~   if (!str.equals("b2")) {7 A6 c0 h: Q* S( g: M3 i1 K! T
    throw new UnsupportedEncodingException(
# h  Y% e# _+ o6 L" p      "File is not in TXT ascii format");
; T4 D  S2 Y% a% b: R   }
: z4 {  w; V& b, W( J8 ^   str = in.readLine();
( ^3 u# [! D8 w5 J. R$ i. b; f4 d   String tem[] = str.split("[\\t\\s]+");/ [; A  Q) o$ D6 Q
   xSize = Integer.valueOf(tem[0]).intValue();
! F# o" S. i' G6 c5 \   ySize = Integer.valueOf(tem[1]).intValue();2 P& Z+ \) x8 ]" f$ Q
   matrix = new String[xSize][ySize];
0 U5 k% K$ b( H; [) e; X  T2 b! W. ?   int i = 0;( j8 e$ \9 @& o+ D/ w
   str = "";
2 z& S% v0 @% g5 F. W7 ?5 Y   String line = in.readLine();
2 e, N( a3 o" x; T, X   while (line != null) {0 w. ~# f  E' }- S4 K: L! r
    String temp[] = line.split("[\\t\\s]+");0 K/ @" ]- t3 z3 G: d
    line = in.readLine();
0 B) y3 B/ r# f7 q; T; r. H    for (int j = 0; j < ySize; j++) {
  b9 Y* T1 {) K2 z& ^2 u2 h  w# f     matrix[i][j] = temp[j];7 z, e$ \& X3 b7 R! ]/ M& N
    }
, _3 A1 Z, ]7 v  k: R: H2 G( y    i++;
  f0 p6 X, L7 t0 N6 Z8 H$ H   }
/ i' g# G0 v, ?6 g. c   in.close();* f5 n8 H! \& a, Z
  } catch (IOException ex) {$ W' i$ L2 e/ U  i
   System.out.println("Error Reading file");
" f# W6 t8 D5 r1 Y- @6 p   ex.printStackTrace();
7 t4 @  a7 [9 h1 m0 G) R   System.exit(0);
9 Y8 O8 p- ]. D  }# H3 V' u8 N! \! s1 ], L
}
3 p! u( A. v, I public String[][] getMatrix() {- q+ y5 s( L$ T0 n
  return matrix;
% I7 T! y7 X2 V: F  E9 a }
% b9 h* u+ _' g}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-19 20:58 , Processed in 0.017638 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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