设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7389|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
( I& f: J! m) s0 S' yimport java.io.BufferedReader;# N' F) }# q8 J' z6 {- T
import java.io.FileInputStream;* B. i$ h; C5 U' \* o
import java.io.FileNotFoundException;6 y0 y! F2 l: o; A6 l
import java.io.IOException;' e* V& W/ |' g5 f1 y$ L
import java.io.InputStreamReader;9 Y: B5 g6 H0 b
import java.io.UnsupportedEncodingException;
9 a# l0 O& y& ?$ z* _. e8 wimport java.util.StringTokenizer;: r+ H6 F% d$ o5 X4 L; P) ^
public class TXTReader {( z+ L+ _  K$ {8 P: T+ x/ `
protected String matrix[][];3 t2 I  R7 Q! k8 e
protected int xSize;  R0 \. u  m* _( x! i$ ?
protected int ySize;3 ~* T: Y# {8 `% q3 R( g3 @
public TXTReader(String sugarFile) {5 V# a+ I2 S7 y
  java.io.InputStream stream = null;
0 A# E; Z$ c+ h! C, X  try {
$ m( y4 D6 q9 ]. e, z+ P) w$ A   stream = new FileInputStream(sugarFile);
8 p3 [" I$ b1 M7 u! i  } catch (FileNotFoundException e) {
! F. a$ v7 S; G   e.printStackTrace();
7 F9 g* k* t9 e  }) g3 Z8 }, m( }9 R" ^, O' B
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));- S2 i! j. G4 |6 y- N6 p
  init(in);
6 z3 e4 {" N) ? }- K( A# Y' e# l% D
private void init(BufferedReader in) {
* Z( H1 n9 z+ \) J: n2 p  try {' r# N& x- s" f! B5 [
   String str = in.readLine();
! {$ Z4 }2 C& e8 Z   if (!str.equals("b2")) {
. j% U# c, k# p    throw new UnsupportedEncodingException(1 ~# X# z0 m1 g, D% n# P
      "File is not in TXT ascii format");/ }1 \1 |; G, {' C% k( z  P
   }
+ c0 C, r0 T6 Q" f/ _( c) V5 ^   str = in.readLine();# {: s! J7 h$ D, V
   String tem[] = str.split("[\\t\\s]+");
2 h) H& F4 ?% T7 ~( z! Y  _   xSize = Integer.valueOf(tem[0]).intValue();' R1 n5 ~- k& [" ]9 A+ M" A
   ySize = Integer.valueOf(tem[1]).intValue();
7 x9 j% v) y1 T2 k9 P- r   matrix = new String[xSize][ySize];+ y. h9 Y1 H. j; w3 B
   int i = 0;2 y0 a$ {) u* y" U. l$ N! f( ^0 T* Q
   str = "";
8 L) k! x0 X& V$ H2 x7 W   String line = in.readLine();  b- _" a. Q% A; S1 W
   while (line != null) {: A* `5 |5 ^" j+ O0 B! [5 j
    String temp[] = line.split("[\\t\\s]+");0 f, K& T; L0 q- x
    line = in.readLine();" V+ v1 t5 J0 S6 y* q; K/ v
    for (int j = 0; j < ySize; j++) {4 x4 `9 N% `4 Q7 ~6 ~
     matrix[i][j] = temp[j];
9 V0 Q4 y' |3 w1 l: v- l0 [% e  q    }; ?5 u7 J5 y0 A7 P! A
    i++;
0 q9 v! e' s- k( q9 M' `   }
, C6 a( j1 d6 l) |% @   in.close();" g% P& v3 x! Y' D$ {+ N& q- a
  } catch (IOException ex) {4 P# @& c( |/ z( g# D2 L' _2 ]
   System.out.println("Error Reading file");
2 N* k* d3 h3 s/ A& R  U4 F1 Z   ex.printStackTrace();
* [. d, t9 E% x4 X   System.exit(0);: j* [9 d, w8 _1 q
  }
" W$ @4 S4 z, ~3 a8 x+ L& b }4 Z  q+ `- f% S0 t# H/ W
public String[][] getMatrix() {
" \: b! m" m' @  return matrix;
$ t6 M+ o4 \; o/ N& L3 u2 u" F }1 B; d) t- E0 v: d8 q2 b- x
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-12 12:17 , Processed in 0.014860 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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