设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10237|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;* Y& s3 u& V) r* A: V" k( d
import java.io.BufferedReader;3 {# {" ?2 J, G  W6 Q6 n& r
import java.io.FileInputStream;
( Y' ~+ `  {# Uimport java.io.FileNotFoundException;! b: g& [& M. y
import java.io.IOException;9 y1 {7 j2 f- a/ K  g
import java.io.InputStreamReader;. y# E0 {9 d: J+ K
import java.io.UnsupportedEncodingException;
$ J6 L; e- n$ E# g5 G& m( D& [import java.util.StringTokenizer;9 J, [# ^8 j3 s$ H
public class TXTReader {+ v" F0 Q# b1 t" R
protected String matrix[][];
& Y$ q, ?: q6 I. L- m protected int xSize;
+ j  @# t- X0 i; V& q  V protected int ySize;
: n! J( P* T' N9 ?- @' i+ } public TXTReader(String sugarFile) {3 d* N7 ]9 o/ F& }/ f$ o
  java.io.InputStream stream = null;- v: [0 d/ y1 N. q1 ?/ W% {& b& H
  try {/ r6 Z- R+ L! P8 K. S4 R* S
   stream = new FileInputStream(sugarFile);
( q( e* V2 X3 d5 c  } catch (FileNotFoundException e) {1 s2 g+ _8 f% x
   e.printStackTrace();
" C  y$ l8 p- K, a. y  }
1 j" F: M; f+ v! }  BufferedReader in = new BufferedReader(new InputStreamReader(stream));! o& T! s4 M3 B% \6 Q9 _8 A
  init(in);+ G& y7 ^/ l2 C
}" Z/ V3 I, p6 ]: D, g
private void init(BufferedReader in) {' p/ v; W& k# k* o
  try {
7 F1 T7 y% q* L% v* f! s   String str = in.readLine();
+ b/ N, v/ A6 F/ J- g   if (!str.equals("b2")) {
$ @; {1 o0 \* Q3 ~" v$ S, [4 T    throw new UnsupportedEncodingException(
# B1 B2 Y6 R0 b$ m' [% h: |% E$ L      "File is not in TXT ascii format");- r" s: `0 ?0 S! f3 \
   }/ E! `* b9 r0 W1 t- O$ H  F
   str = in.readLine();
+ j, c: O+ M8 _! ]% x/ B   String tem[] = str.split("[\\t\\s]+");
+ R  ]- f9 a5 V/ _  p% F6 x- M: z4 z   xSize = Integer.valueOf(tem[0]).intValue();
2 B) z, l' w; \% O   ySize = Integer.valueOf(tem[1]).intValue();
0 C; J- K: {+ L+ J( S% G   matrix = new String[xSize][ySize];# q4 }% H2 H* m- [* ]9 S
   int i = 0;
! {: W. r4 W  z, Y3 O) Q. [: {) S   str = "";
* d# m( p9 D  P1 ]   String line = in.readLine();" e$ @" Q% V% ?2 b  G* Z
   while (line != null) {
4 I: Y. }9 |/ k    String temp[] = line.split("[\\t\\s]+");( u8 N& C- M+ X0 c2 Y; K2 L5 L1 {+ w
    line = in.readLine();
  ?) F' `% F; H    for (int j = 0; j < ySize; j++) {
1 c" L6 d' M" c* ^8 r     matrix[i][j] = temp[j];) Z, D/ ]8 c0 I6 F; Z: k
    }
# A- U5 y& j2 g' E  {    i++;$ G# R! T( c) B! ~. }
   }
2 g1 v) n5 X0 U* t# G* ~) x   in.close();& S  D0 I: D6 @8 O% J: _
  } catch (IOException ex) {
" O! n8 o* n$ R+ A0 x   System.out.println("Error Reading file");
. b8 `6 F4 I: H: v   ex.printStackTrace();* w! p, I, X3 D$ D! i" x
   System.exit(0);
0 B5 |2 D. }* A* h/ e+ U  }( W( C4 [2 [' W4 V8 D- I7 c
}& \" m# d. X$ |0 l
public String[][] getMatrix() {
4 M8 U$ D( T! O" g6 ~9 u/ w+ A  return matrix;$ G7 I8 X7 g  ~- h- ]5 v9 i: ?6 B
}
/ t5 d% h! M/ \3 C6 y- Q8 y3 H}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-30 12:42 , Processed in 0.024285 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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