设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9032|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;6 X# y$ w$ I( k' z* M1 s# L, j+ {
import java.io.BufferedReader;
) U' e& I+ ^: H" t" x6 |& Eimport java.io.FileInputStream;
; q/ q! o2 J4 U$ himport java.io.FileNotFoundException;
+ z* O' h6 Q) n2 K$ t# s" `1 P. U3 f2 iimport java.io.IOException;% @5 Y0 l" V% H& G, f2 H
import java.io.InputStreamReader;2 w/ S8 I" x4 M  E5 {: H; O
import java.io.UnsupportedEncodingException;$ L/ {- k# d1 {4 c
import java.util.StringTokenizer;" e1 G" d9 ~- ?: h+ ]
public class TXTReader {+ S; ?% L: g1 _' b- u) u: y
protected String matrix[][];
5 Q/ D( V9 T" h  v& y7 V protected int xSize;2 M& l& v9 s2 R8 V
protected int ySize;
1 _! h: I  W3 S. [# T- t public TXTReader(String sugarFile) {
/ u4 `1 d$ G1 k3 r* a+ X  P  java.io.InputStream stream = null;
: j- A8 Z' I, J4 x  try {+ S& n. f5 o9 D9 i
   stream = new FileInputStream(sugarFile);
- L; `: A2 m  O3 g$ s' V  } catch (FileNotFoundException e) {9 d7 \+ f; J. b
   e.printStackTrace();
9 [9 Z% W- F- Q  }7 v6 F% d: c& E! @
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
3 B6 I6 c) J# T4 `# B: z  init(in);3 d) Q6 P! q1 G& [; d
}
5 P5 R8 E! Y, S6 m0 ` private void init(BufferedReader in) {
& C- C, j0 R( C8 K6 S! V  try {4 h7 d9 U& A6 B* n$ h
   String str = in.readLine();
3 U% Z7 S! j& M% w' d6 K   if (!str.equals("b2")) {6 g2 ~9 Q# t+ z7 g8 O$ R' M
    throw new UnsupportedEncodingException(5 g% K# \5 {/ \- @
      "File is not in TXT ascii format");' g: B: G" E1 k2 P' I3 ?
   }% I, d) l% c6 ^% F
   str = in.readLine();
8 y; \2 E6 w( |  K$ m& J4 n   String tem[] = str.split("[\\t\\s]+");
8 \  v: B) g$ ?/ @   xSize = Integer.valueOf(tem[0]).intValue();8 K3 o# h! W, S  M4 s1 O
   ySize = Integer.valueOf(tem[1]).intValue();# X* I7 Q1 F$ N3 ]  \! o
   matrix = new String[xSize][ySize];. f6 {2 Z$ ~2 l( b/ i9 y
   int i = 0;: F# d! F# F6 z$ Y) d3 z! [
   str = "";, z  A. f% j% b
   String line = in.readLine();/ [( g" d$ M5 u% l0 k0 q1 r% ^( y
   while (line != null) {3 S$ Z& S( j: n1 ]# Y  R; g! `( d) Y
    String temp[] = line.split("[\\t\\s]+");
6 W7 Z  e0 |8 @. Y# v    line = in.readLine();/ J+ x/ [9 n" O6 k6 R
    for (int j = 0; j < ySize; j++) {8 w6 \) U' u$ R) F
     matrix[i][j] = temp[j];
* {3 \2 X" s# ?$ u7 [    }
6 [* }" `  @1 y) ?% y* K    i++;& n+ O; _- I% s
   }
, P5 o# ~6 o. R8 D  A0 k: }! F   in.close();0 ~  W8 m" o/ L( _
  } catch (IOException ex) {% K4 _2 x% L7 M  t8 o$ Q
   System.out.println("Error Reading file");
% P1 _' S& a. ?+ f$ u6 s; p   ex.printStackTrace();$ t# J2 q1 j0 U# g6 _
   System.exit(0);0 o: Z( X; V! R# S7 X8 N/ I; E+ D& `
  }
2 |( t; \. Z9 ? }
  |0 D6 E/ f) {7 t5 o7 ^ public String[][] getMatrix() {
+ u6 F- b, F& T# n# N1 N/ {  return matrix;
1 d! E2 B# u0 l: `, A. T+ \ }
+ C) s9 U; A7 _7 u}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-27 20:21 , Processed in 0.014339 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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