设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7871|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
, H6 Y. S, w& `) mimport java.io.BufferedReader;
- V% e. o9 ~4 E; ^8 h4 Mimport java.io.FileInputStream;' ^5 e% o! a1 Z5 }' ?9 C+ ?, q
import java.io.FileNotFoundException;# y% {0 Q  X. V8 K0 p, D1 ]
import java.io.IOException;( D7 [: b: t1 C+ @1 Y: W
import java.io.InputStreamReader;# U0 [  n0 L8 J4 F
import java.io.UnsupportedEncodingException;5 X- u; G& r; y+ c' a- K- V: K
import java.util.StringTokenizer;
/ J$ X4 b5 G& @* u: {6 _public class TXTReader {' V4 A0 c7 P0 `! \% _% I! y) T
protected String matrix[][];8 m/ q+ ^1 G7 `7 x
protected int xSize;
2 J  i% E( B1 Y( }; q protected int ySize;
5 h4 E  v  M, _) r& z. \6 ]8 B, k public TXTReader(String sugarFile) {
5 @, G9 O$ ?" p  p3 h+ d  java.io.InputStream stream = null;
) h" J; ~7 J4 U: s- e  try {  t& k) N/ }- u' j
   stream = new FileInputStream(sugarFile);# U7 {4 ^! j" x2 l% k4 o
  } catch (FileNotFoundException e) {
( N& g  H8 D  @   e.printStackTrace();9 E2 z" \' {7 o, C# R
  }
' s6 B9 u1 ~7 _: h  BufferedReader in = new BufferedReader(new InputStreamReader(stream));. e# n( i# w$ Y
  init(in);
6 Z- @  O$ k) O9 e6 w4 S2 O }7 W* P( e! {* j( q9 h7 U/ N, I0 P
private void init(BufferedReader in) {) z8 _- x3 N0 y2 A9 v' t# V
  try {4 `" Z  H- {1 ^; ~/ r4 a& m4 p
   String str = in.readLine();/ p! `3 b4 y" r% a! B
   if (!str.equals("b2")) {
! k: Q& }" l& u    throw new UnsupportedEncodingException(
  Z" t, C4 X+ @7 P' G$ _      "File is not in TXT ascii format");# u/ E# l* G! X( l
   }; Y! F* I  ~# u* ^! G
   str = in.readLine();
0 M; o- ~/ U+ J' k  `   String tem[] = str.split("[\\t\\s]+");# P& D# L9 w; G/ y
   xSize = Integer.valueOf(tem[0]).intValue();# J: H6 s* I- ^
   ySize = Integer.valueOf(tem[1]).intValue();1 p6 w7 c# P+ l- Z5 ~& r1 F* z  z
   matrix = new String[xSize][ySize];
, N% B/ T0 O7 k2 J   int i = 0;9 m3 }& {8 v! Y6 w. m9 E; x
   str = "";" f  t9 ^: w8 M/ L" k
   String line = in.readLine();
# P7 E* i9 d/ a. ?# ^0 ]& E   while (line != null) {
& y* V% ]8 l0 M6 i" p) q& q8 `    String temp[] = line.split("[\\t\\s]+");
" m% I" G  s; ?, t, [: s    line = in.readLine();
! \7 h+ [! `8 x$ O    for (int j = 0; j < ySize; j++) {* ^% H& I0 l8 t5 y8 `
     matrix[i][j] = temp[j];1 n$ n0 Q' z6 E# E2 S( V
    }* f  H6 I; _# S& L
    i++;% D# u0 X$ G  [) V
   }
1 W: G$ d0 b/ K  z  u* q# J   in.close();2 b8 H) [' U4 J
  } catch (IOException ex) {! p: d- X4 I: s
   System.out.println("Error Reading file");
, k- K% c& ^$ ~' q   ex.printStackTrace();
  _3 r3 M# C! W; d) s- @* f   System.exit(0);
) N0 a: ]6 m0 m/ {) ]  }
! j/ L, Z, G$ ] }
$ q7 t6 h8 r- ?; x public String[][] getMatrix() {6 z1 T! d9 l$ |/ j0 ?8 L9 c
  return matrix;$ ~9 L( V- b' r( o. m% h: c
}0 L6 E4 n1 C& r: {4 o
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-19 05:42 , Processed in 0.051032 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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