设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8305|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
$ L- U* u/ M: }import java.io.BufferedReader;. N. L; q- s  ?' K
import java.io.FileInputStream;
/ }# v: |4 X( ]; @3 Dimport java.io.FileNotFoundException;
8 p' {; p2 j% p' Kimport java.io.IOException;
: P- \, c% F3 X+ S5 v3 jimport java.io.InputStreamReader;
( D4 n3 ]+ {! w; N" Y( ximport java.io.UnsupportedEncodingException;/ q7 o% @2 H8 ^; w3 \% g* W
import java.util.StringTokenizer;2 k% V3 O' Y( q. ]2 l, {9 y. D9 i5 {- s
public class TXTReader {
& w8 y( S; A  N' [+ h protected String matrix[][];
7 P' D3 F; S6 ]; T protected int xSize;; @% q7 E- J0 ~2 s, |) m1 ?
protected int ySize;! l+ l: t* |; ]4 k
public TXTReader(String sugarFile) {; h7 ~' R+ {! [" x7 a5 o
  java.io.InputStream stream = null;1 b( V$ q# E$ d4 l
  try {3 G5 E  L: {1 p- c- p
   stream = new FileInputStream(sugarFile);
/ [6 p$ S7 S. v) j& N1 `: E3 t$ p4 }  } catch (FileNotFoundException e) {" T$ _3 y4 |4 N% H' H( K4 }: L: R
   e.printStackTrace();
2 s/ J8 r% S) d3 p& l; D  }# Y$ m" t8 z+ ?& D! U
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));* d; c7 r5 y" W/ a
  init(in);
6 u: E/ s+ O" H9 o' j( h) V }- F5 P7 j8 i# e- r
private void init(BufferedReader in) {
/ U3 f% Q+ y: ^4 C  try {
4 ]# h# N6 y: @6 O  |0 }& u   String str = in.readLine();
) E5 A7 o0 m; G  e  o   if (!str.equals("b2")) {
9 G1 J1 a4 d6 k$ y+ q4 g    throw new UnsupportedEncodingException(
/ g* x! g% d6 L, I* V9 [- j      "File is not in TXT ascii format");
" A* H# M! ^/ j$ h- p* e: `, k' N( \   }
2 o8 a' E4 B9 w0 |1 R5 B, D   str = in.readLine();0 r# b5 Z$ P7 O) C
   String tem[] = str.split("[\\t\\s]+");  A; |, H. c$ N0 E4 a
   xSize = Integer.valueOf(tem[0]).intValue();
0 s9 Q6 H/ @; l/ S# ]0 X0 H   ySize = Integer.valueOf(tem[1]).intValue();
% I' J: f# s/ k0 Q   matrix = new String[xSize][ySize];
* j, Y2 K4 a( A3 [+ I3 ]# f   int i = 0;
5 l$ F* M$ Z8 D& j   str = "";
1 y$ ]' G! |; N3 R$ z) \   String line = in.readLine();
0 p3 \4 j; g+ r: [7 T1 L   while (line != null) {
4 P8 i9 \8 u( X0 u5 K: D  i    String temp[] = line.split("[\\t\\s]+");7 i0 \- y* ]! c8 b' _# P3 [
    line = in.readLine();
; ~7 G3 a! }: Q    for (int j = 0; j < ySize; j++) {
  r4 _* ]2 R+ S) N1 I5 E     matrix[i][j] = temp[j];
! e( n# `( ?5 u0 C3 R1 C4 k3 A    }' g5 ~# Q; G! L$ w: G% o
    i++;* \3 G1 T2 @3 o* j/ ]* D$ n
   }$ P; f% c/ \/ c& `: i
   in.close();
) q/ p( J4 J) v* {  } catch (IOException ex) {: s9 m- v+ v: `% @' |/ l5 w7 [
   System.out.println("Error Reading file");
( f; c, Q1 H3 B/ \! C4 f4 X' g- N   ex.printStackTrace();
4 y/ P4 O1 [! T* ]9 Z   System.exit(0);/ @1 j! l5 s2 r* ^& u
  }5 i0 Q" y/ e6 E, u! A' [
}$ g/ a" H' \- ^
public String[][] getMatrix() {
9 B* s. \% G5 ]$ W- R$ K9 p  return matrix;$ W. `8 E& Q! f/ {( n
}
& f0 F. u* O. l% R, a( H( n' x}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-14 05:19 , Processed in 0.017878 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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