设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6601|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;( T  O- `! \/ M; l
import java.io.BufferedReader;/ T; b  |! j1 G* U* c9 Q
import java.io.FileInputStream;& V* c3 M$ o0 Z: Y* ]. `- P3 ]: G
import java.io.FileNotFoundException;; w7 [0 ^, I* C
import java.io.IOException;
0 F+ g; [4 E, D7 V9 s# D2 mimport java.io.InputStreamReader;
% N7 R6 u' Z& K; }8 N1 Y1 I; ximport java.io.UnsupportedEncodingException;
" o# S8 [6 F7 {! O. simport java.util.StringTokenizer;
7 e+ N( ]8 `: D% q, b; c# T( ?public class TXTReader {
; j6 ^$ s- L  w8 p* U: @4 N2 X protected String matrix[][];) F4 ~# x" q+ Q) O8 P
protected int xSize;
: q; K+ W# t* L protected int ySize;
9 A8 a" ]! p+ [; c4 I public TXTReader(String sugarFile) {
7 {: }3 g; M8 S0 Y9 H  java.io.InputStream stream = null;
9 A; G7 h  q7 n( D$ \  try {
/ k  y3 Q9 r; b0 q4 {   stream = new FileInputStream(sugarFile);: |  q3 H& d0 O: [" v1 f; h
  } catch (FileNotFoundException e) {) c: D( W' ]- c. }9 {0 ?
   e.printStackTrace();6 V, u$ s5 \# F8 a, Y' d  m: X
  }
( I; I+ B: ]6 B9 U7 X2 ?  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
# |9 x5 R0 }# D1 ], m1 a  init(in);; f7 G8 _8 {0 x" R( s1 w9 j
}# z6 i( v1 v/ H- I0 V2 X
private void init(BufferedReader in) {
5 C/ z$ a! T0 ^5 ?9 ]3 N  try {/ U- o- p5 [/ i. I) A" `' |! V
   String str = in.readLine();
: @9 @# V* x$ d% W+ V& h   if (!str.equals("b2")) {
: Y! g, k5 ?! S% R- h8 K    throw new UnsupportedEncodingException(
/ S& Z4 M( z2 V/ @8 {: |: X$ x1 ^      "File is not in TXT ascii format");
! X% K! J: }  C4 H  S, E   }/ v# C- H& p6 l3 }2 W) b6 ^
   str = in.readLine();3 T: i- {! c0 b: h; w% R% r
   String tem[] = str.split("[\\t\\s]+");
5 Z) R: V. b8 a7 n4 n6 w- j   xSize = Integer.valueOf(tem[0]).intValue();
. r) G. p$ B! d2 O   ySize = Integer.valueOf(tem[1]).intValue();0 q5 X- z! M8 f, O& K
   matrix = new String[xSize][ySize];
$ L! k0 p  g  j, c# Z' A   int i = 0;
) {1 [; |6 o$ c, U" n5 \   str = "";
' [( a5 e6 v: h+ G7 I  {# t+ H1 s   String line = in.readLine();, i3 g5 K% T+ t* Y" o+ B- b
   while (line != null) {
( {3 D0 S0 s' ?8 G& [    String temp[] = line.split("[\\t\\s]+");
% T! c9 w# Y6 J    line = in.readLine();" y+ f# C/ ]6 f) _; o7 _
    for (int j = 0; j < ySize; j++) {  f4 ]& U+ \: J; h- s3 I
     matrix[i][j] = temp[j];
3 B1 W" H& T4 {% R/ d    }' E* ~0 s0 B/ `% u
    i++;" H! e8 S& p# i0 Y4 N& t2 i
   }
! ^; v1 q+ e! d0 F8 R   in.close();
; p8 _/ I" w$ C- ?% \  } catch (IOException ex) {
2 N% }9 t8 ]. q1 f' m( E+ X   System.out.println("Error Reading file");2 y. P1 ]$ T. ~2 S' Q2 S
   ex.printStackTrace();
# u- I8 g  f* w6 K- A7 v   System.exit(0);
8 E5 f$ Q. @  m5 o) L0 S, W  c  }
- i. i( J7 {9 b; N8 i" j }7 v2 k5 A: S9 c- Z+ {2 J
public String[][] getMatrix() {  b5 u3 N; d: v- @* `; b  Y
  return matrix;8 U8 ^; U3 S; W: t% H
}# M4 [3 \4 h" ]# k
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-21 05:25 , Processed in 0.017738 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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