设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10336|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
$ J+ u* f: U- G3 ^import java.io.BufferedReader;2 ^$ {: w% q' k# }0 {% d6 Z1 h
import java.io.FileInputStream;
2 f9 E( G8 u4 M2 H. e. mimport java.io.FileNotFoundException;
# y- r/ [! ^; ~7 [  X7 [0 s5 _( limport java.io.IOException;) E8 u5 G) _8 ^1 b
import java.io.InputStreamReader;
; n. z: O: _7 qimport java.io.UnsupportedEncodingException;
2 |, J, X# T/ ?+ \import java.util.StringTokenizer;
+ x4 s6 [0 \& h* E; Mpublic class TXTReader {
8 P. i/ G: W3 m1 @: U2 j0 [/ g protected String matrix[][];
' [+ p9 ?  H- b3 [* e  y protected int xSize;
3 w1 f6 p9 D" q) v* _9 G; m) q protected int ySize;- c" m& |$ ?, ~
public TXTReader(String sugarFile) {" M! z, e; q2 v# @
  java.io.InputStream stream = null;* t& r! G) u5 k: m$ B
  try {
7 e2 F. R6 x7 F7 I' @   stream = new FileInputStream(sugarFile);
# ]' P6 t$ z7 v' t, D, X, W$ H$ W  } catch (FileNotFoundException e) {0 P+ E5 M) b# k5 J
   e.printStackTrace();& O  H8 _: \5 h# q; f
  }# e9 e( [) |% z8 N3 @2 I7 c0 r
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));! l$ r9 l; u6 k) e
  init(in);
2 m! n/ T% h  o4 w/ O- D }
9 N/ U: g8 L* T private void init(BufferedReader in) {7 L  ~1 I: Z8 X% _( G1 C. _& x
  try {
  y' }- z$ M3 k6 S3 j   String str = in.readLine();
+ k) q. Y# @7 M   if (!str.equals("b2")) {
; C4 p! @6 ?0 m$ S4 M& |    throw new UnsupportedEncodingException(
8 G7 Q, o7 _; `% q" X" \% d1 D      "File is not in TXT ascii format");4 b7 i2 N, n: W2 T9 Q
   }
2 [8 F; l6 l  \0 x   str = in.readLine();/ J! X. m0 E7 G4 }8 H
   String tem[] = str.split("[\\t\\s]+");! [, R* N% q# V1 @
   xSize = Integer.valueOf(tem[0]).intValue();
9 \$ w- w- `5 O. t9 P   ySize = Integer.valueOf(tem[1]).intValue();
% p9 s  H- T2 g8 O   matrix = new String[xSize][ySize];* o8 _& t- {  v
   int i = 0;
! B" P9 ~2 V& n" u$ G9 s   str = "";
2 k6 `" S# a; \7 u) f2 g   String line = in.readLine();
( X2 f  d) B5 P$ N0 Y$ N   while (line != null) {! ]( O. q6 ]' U" k* r$ _
    String temp[] = line.split("[\\t\\s]+");8 U8 Z; W( }, e/ ~
    line = in.readLine();
: Q% }1 L0 p. e* Z$ r6 @( N    for (int j = 0; j < ySize; j++) {' J3 ], O$ G9 |
     matrix[i][j] = temp[j];
* ?! G+ d# r! F# b    }% S" S/ B% o; Q9 J8 M* A' o2 O
    i++;" i! n) E& o0 _4 x( d
   }$ ]3 Q2 A4 s4 `5 I7 ^8 S
   in.close();# T5 e5 V% z1 i- C; T4 l
  } catch (IOException ex) {
- X: a1 V( K( C( g, s& |, s   System.out.println("Error Reading file");" K  E; F8 g* |, L" q$ |1 A
   ex.printStackTrace();
2 l+ f$ Y' f) L$ [7 s   System.exit(0);
' G8 _/ A& Y0 _  }' D* @& ^0 ]. |
}
  P" l/ T; E0 O public String[][] getMatrix() {
6 h, D4 B  z+ a+ j6 N  return matrix;
1 ~5 x: m% D. T' a3 _: b }% s" V" y3 r; B5 H
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-4 22:39 , Processed in 0.017248 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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