设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9814|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;+ n4 k. a  `9 k+ w% [2 a; F# f
import java.io.BufferedReader;" Q/ x/ E$ O; ?. P
import java.io.FileInputStream;& U5 B, n3 d2 i, I& g! G
import java.io.FileNotFoundException;7 Q$ U( l( V; P& i' s9 J, P# q; z. E
import java.io.IOException;
+ h# q7 G6 |1 F7 Bimport java.io.InputStreamReader;
" Y6 Y% O* H, ^; {  Vimport java.io.UnsupportedEncodingException;$ A, Z/ h; ~/ Q
import java.util.StringTokenizer;
8 `" H! X: G) |5 |public class TXTReader {% d2 ]) }% x' j6 S
protected String matrix[][];
( O8 z' W( `4 J protected int xSize;
' Q) I* X" q+ @ protected int ySize;
8 i, J. j# K0 Y1 a( V& I6 r public TXTReader(String sugarFile) {
0 F' s; C' I& R1 s  java.io.InputStream stream = null;. i4 O$ D5 m1 O' j' o
  try {0 @# R4 C6 j. n4 F9 `
   stream = new FileInputStream(sugarFile);
9 y/ Z& W' J, s- {  } catch (FileNotFoundException e) {
9 V( u+ J& p/ o. P9 p   e.printStackTrace();
" p) d" W5 c2 p' D% `2 {- D  }# P" M( {% s7 F/ J! v. D
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));1 L( z! B; h% D1 \, |7 M( V7 _5 W5 y; s
  init(in);
% w3 P, k' C9 a }8 B# C) J6 {* Z1 ^1 v3 \8 B3 |
private void init(BufferedReader in) {1 N: i: g3 d7 m$ J- R
  try {9 o; \2 ^2 S$ _9 [% j- H2 `- ?1 m
   String str = in.readLine();
2 B0 T% t, S: N) a( Q4 z   if (!str.equals("b2")) {0 `( u2 z9 g& `& O, p; U
    throw new UnsupportedEncodingException(( }  y' C- H/ \4 d( a( c: Q
      "File is not in TXT ascii format");
! \  p& }4 B7 L# |' Z0 u   }
% o& n: \& [. z; e" P   str = in.readLine();0 d; x' M0 C  M: [8 [' R
   String tem[] = str.split("[\\t\\s]+");
9 h3 {' \7 Z; F. e( |: x  O   xSize = Integer.valueOf(tem[0]).intValue();) t# O& O0 n6 k
   ySize = Integer.valueOf(tem[1]).intValue();' u- b  `" [8 f6 E. n$ K
   matrix = new String[xSize][ySize];
/ c6 D4 I! H! n" ^4 j7 b1 h) P   int i = 0;
5 R7 ]" _8 I4 u( }0 }% q3 F$ c   str = "";
6 [: C5 l9 @7 h% q' E   String line = in.readLine();) x, w* ]2 ?/ M2 |1 ^
   while (line != null) {( J7 H5 Q# n2 F  o
    String temp[] = line.split("[\\t\\s]+");
$ X0 H) K! u/ w    line = in.readLine();; u/ A3 Z8 A- {3 i9 n$ a; t1 O
    for (int j = 0; j < ySize; j++) {9 N* V; C% d9 [2 a) c
     matrix[i][j] = temp[j];7 \4 ~3 Z# k. W5 ?$ l8 P8 P
    }. {) u/ ?1 S$ Y0 {
    i++;' k( H1 c* [5 G: L- }
   }
8 d+ a, {; ^  ]. }2 j   in.close();) I7 Q- {0 \  k: I
  } catch (IOException ex) {
2 n& E  X8 S4 }3 X   System.out.println("Error Reading file");  N, Y* E. \% b2 i/ J4 h" C
   ex.printStackTrace();
0 h; f; b% @) i; r" F   System.exit(0);
9 u; r# w  w% Y# v  t( r4 e8 v  }; o; K" E5 ^0 i7 s! ?9 c" Q# I
}' Z/ {7 w1 h0 A  C7 V
public String[][] getMatrix() {
# B' p3 `$ s8 F! i+ C  return matrix;5 K0 }! {/ ?) V* `, Y
}
4 f1 }7 z5 |; z4 w: I}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-5 10:00 , Processed in 0.015422 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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