设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5007|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;/ R) E* I3 L4 p( g0 y2 I
import java.io.BufferedReader;4 c. M+ B: t$ e5 y8 p. k- t8 w
import java.io.FileInputStream;$ Y9 d9 M1 {, O# r& a  b
import java.io.FileNotFoundException;
' i" N4 Y4 J! I! A$ i' eimport java.io.IOException;" ^! A' j3 h6 |
import java.io.InputStreamReader;
: b7 D, m2 C3 g- v" c8 w! ^import java.io.UnsupportedEncodingException;& y+ Y/ q* O; k& o
import java.util.StringTokenizer;
7 K0 S; S  E3 `- e+ S" `public class TXTReader {
9 f( _8 @* h4 n0 r* r" } protected String matrix[][];
$ T+ m6 Z  q1 j' T protected int xSize;
1 _) n% n. m3 e8 a protected int ySize;. N9 b; _2 l- }# B! j  o
public TXTReader(String sugarFile) {
8 V3 b; w, f( H  java.io.InputStream stream = null;
2 @5 G! A2 w0 G! Y+ o& u  try {1 g& ~& O) H! X9 \, j
   stream = new FileInputStream(sugarFile);
, c$ \# x+ W$ E/ |, S' H  O; Q  } catch (FileNotFoundException e) {5 Y6 b6 Z. W3 Z5 q9 N  s/ I5 {
   e.printStackTrace();, r9 y9 a, z8 |( ?0 l. |3 u
  }
4 B% `- Y# v0 A8 L7 Z( ?5 T  BufferedReader in = new BufferedReader(new InputStreamReader(stream));/ O- v/ @+ |' q) @' C- N: G# m
  init(in);' u5 G; J" u+ z# z
}3 h! E; h- }4 w+ Z- n2 p/ s* V
private void init(BufferedReader in) {5 b0 d5 B7 ^" b1 g; J
  try {
7 N, l. R9 H/ u. u* _- n   String str = in.readLine();6 z- T. k2 M/ h( v8 y
   if (!str.equals("b2")) {
) v* Z4 m9 c" R2 I5 W9 H    throw new UnsupportedEncodingException(' E! q+ Z; s2 C1 z' @
      "File is not in TXT ascii format");3 ?/ X( N! N: Q( e7 B, C: h
   }0 n. t5 U3 J" B6 H
   str = in.readLine();
. l, ?8 L- M" g/ ?* A. l. b   String tem[] = str.split("[\\t\\s]+");7 x8 ^1 M) W) H/ G+ k. `
   xSize = Integer.valueOf(tem[0]).intValue();
2 P. L+ Q' Y% x   ySize = Integer.valueOf(tem[1]).intValue();2 t+ u5 w) z2 n( o8 Q. {
   matrix = new String[xSize][ySize];* {0 r( y; Y3 `" e' P, x
   int i = 0;9 L% Z! L4 p& W$ u+ q0 a! W
   str = "";8 {$ m% F; X6 c( c. k1 M
   String line = in.readLine();
4 G% `% ~! n, C. O  ~' g   while (line != null) {3 k$ a6 }4 A& c% ~7 r, B6 }
    String temp[] = line.split("[\\t\\s]+");: c& A: b2 E, v& i3 @
    line = in.readLine();; f! Y  I  ~# _
    for (int j = 0; j < ySize; j++) {2 @0 N* X% n2 o( [; A, D: D" `
     matrix[i][j] = temp[j];
  N5 }5 V& X7 w6 n& }    }, q) P' U3 J9 S0 E! _2 E
    i++;
  l2 |5 L+ d1 c   }/ I" @& r4 B7 C8 g
   in.close();9 G6 b4 k& j$ S3 S& |; n
  } catch (IOException ex) {; m. R2 r: Q) t' n" t
   System.out.println("Error Reading file");* `5 k& s8 l" \% b  m3 L; w# M
   ex.printStackTrace();) \8 q, ]7 x+ P2 d& g
   System.exit(0);
* h% J9 H3 y" u  }
. z; a' b( L4 [* l }
8 T7 ^7 }1 B8 J' R. S: F/ B public String[][] getMatrix() {
+ |+ A3 q9 Z! D0 C  return matrix;5 \) d6 d4 U' [: T$ A0 O5 D
}
2 F% `( F! o1 M  Q8 y, O}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-7-7 16:16 , Processed in 0.014408 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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