设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5600|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
  K8 d& E, d/ |import java.io.BufferedReader;
; t( O9 ~1 }# Z$ w" iimport java.io.FileInputStream;
! r* r5 o$ G: W5 M; j1 E' U% dimport java.io.FileNotFoundException;& w( y# g7 \8 w* n
import java.io.IOException;
& u( p- T& U: {) u& ~import java.io.InputStreamReader;7 u6 L8 Q" ^- N/ ]5 m) J
import java.io.UnsupportedEncodingException;( a  C9 k8 W2 R' d
import java.util.StringTokenizer;
$ M/ S" F! g$ o/ dpublic class TXTReader {
- k: [; K/ p4 ^% v, E protected String matrix[][];& V. j# N/ Q; F- c- c7 z0 ?
protected int xSize;6 q( r8 f& |, ?5 }( W/ \2 S
protected int ySize;/ e* c4 s: U. N2 ?1 U# s2 o
public TXTReader(String sugarFile) {8 P1 {6 S9 w1 t- ]9 F' j
  java.io.InputStream stream = null;8 o+ h% U( U' s! q) t' y
  try {" p; `  x8 O! p: n
   stream = new FileInputStream(sugarFile);
) \2 e$ O- I! y* P2 b  } catch (FileNotFoundException e) {
3 X1 r' c4 p* s   e.printStackTrace();
0 [5 ^0 D  r: p) U& r  }
, x8 }; @! t" s. R! _0 R% K  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
* B3 H) `5 q/ Y& c2 e9 @  init(in);
9 Y: z1 v+ ?, f }% `3 D! x7 r; P- v* {
private void init(BufferedReader in) {
3 d7 }$ d$ n* P- n% }  try {
( V+ G9 J& U  G9 J1 Y* i   String str = in.readLine();; `! H2 w) V$ x0 ~
   if (!str.equals("b2")) {
/ ^$ t  a% j4 U+ U/ r    throw new UnsupportedEncodingException(
" z$ f+ O0 e) e- f3 e  i4 B      "File is not in TXT ascii format");
* {4 `3 Y7 u' [# m- o( t5 u   }
3 Y9 Q+ o" e$ P/ P) R9 I' D   str = in.readLine();
: ]! w8 \" M. A3 ?( l; J# D   String tem[] = str.split("[\\t\\s]+");
$ j2 p9 N- L$ @2 L2 D. p! B  F   xSize = Integer.valueOf(tem[0]).intValue();
' q3 W+ F& @6 q7 f& x   ySize = Integer.valueOf(tem[1]).intValue();
7 o4 R& h3 B1 b( |$ N% P2 s" ^   matrix = new String[xSize][ySize];
# p. ?" x' j7 [: i1 V3 i   int i = 0;& \  z$ Q: }) X7 r9 `2 r5 P
   str = "";) k) L5 q2 y$ I
   String line = in.readLine();
' P: J0 c$ a! M% K; j   while (line != null) {9 m0 Z4 M1 k) u# o" g  _
    String temp[] = line.split("[\\t\\s]+");
/ F+ ]- L* j7 T, }$ f    line = in.readLine();
9 F. F  J' O: {( _) R5 x( Z    for (int j = 0; j < ySize; j++) {+ w- B) D9 O4 M: U# H$ }0 a
     matrix[i][j] = temp[j];8 f6 W- {" Z$ v% C7 I0 j
    }# o4 e5 R5 J% i% R
    i++;; v+ y" I; I3 c. P3 w
   }
% z- d9 L/ i8 w( s   in.close();
2 L, Z. R3 U- P) q9 I  } catch (IOException ex) {/ }' D* _% v; V" J
   System.out.println("Error Reading file");8 U$ s: B) i& q0 q0 d$ a
   ex.printStackTrace();
+ n( K  [- z5 @$ s3 Y   System.exit(0);' C; ^3 `2 F- o3 p% w
  }3 c( s7 |3 M) W5 K5 r- g9 p4 N% u8 a
}
9 r; o2 s7 {* n3 u0 A5 Q  a public String[][] getMatrix() {
" N4 I3 E0 \3 _( B5 ~$ r  return matrix;
& k' {1 M4 V- E3 e6 x  O' @ }
3 g5 H0 c( l! X) p, V# k}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-13 16:44 , Processed in 0.015247 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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