设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9938|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
- y+ z  V7 k: {  r0 K; Mimport java.io.BufferedReader;2 C% [$ u, j# H3 \# c
import java.io.FileInputStream;
9 m4 G+ w% t! W1 p  w- {import java.io.FileNotFoundException;
* {4 e" t! q( C+ l& z, Dimport java.io.IOException;& z# k6 L& Y7 Z2 z. b
import java.io.InputStreamReader;
" l, x2 s0 D" Cimport java.io.UnsupportedEncodingException;, k/ q5 n1 P, ]9 }( v
import java.util.StringTokenizer;* n, c9 _% v4 u, v. F
public class TXTReader {( W9 E# t  L- ~" P% }
protected String matrix[][];" @& e. a# }- e% X1 H3 ?; R- h
protected int xSize;2 p0 x6 A; d4 |" E2 W6 O# q
protected int ySize;. z& P# N5 U5 @# ]  v" N& [- ^
public TXTReader(String sugarFile) {. i8 t5 \3 q6 A5 l7 e
  java.io.InputStream stream = null;
$ b# E5 m. c2 T3 F6 U# T  try {+ f: i0 |% V  E5 O; ]9 A6 o
   stream = new FileInputStream(sugarFile);4 k% d  g2 s" X* n2 B
  } catch (FileNotFoundException e) {
$ ]$ A2 X* _- @6 w+ o3 a8 z1 [) Z   e.printStackTrace();
+ a% O, C/ E* L( K  }/ ?( }: G" Y# ~5 I
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));3 `8 ^5 B! [' a* P! m- G' `: l% Q
  init(in);
) ?" D& N) u# ` }
1 }# Y- b% d; y* h' m1 V% t private void init(BufferedReader in) {
0 S7 J+ B4 y# ^- h( E6 q/ ?  try {6 V5 e5 r# G5 Q4 s8 S* `5 ?
   String str = in.readLine();
7 R. S% B1 u; G   if (!str.equals("b2")) {, ~, B( x; W  {5 G& T: |. L
    throw new UnsupportedEncodingException(( G. X) t% g; c4 g, L3 d, k- W' J
      "File is not in TXT ascii format");
+ s  [6 ], m; i   }
9 B1 R8 u2 @: M3 B! W   str = in.readLine();& E& m9 h/ [& V  z
   String tem[] = str.split("[\\t\\s]+");
/ i' c5 J- K# [4 R# q   xSize = Integer.valueOf(tem[0]).intValue();8 V* M  P0 F$ a! `
   ySize = Integer.valueOf(tem[1]).intValue();& n, D' V2 r$ D! B9 s3 q& c
   matrix = new String[xSize][ySize];
+ l0 O% G" E3 ]" Z   int i = 0;6 Y/ ^: c7 o! y  H
   str = "";
" w( g- a! w+ m9 \   String line = in.readLine();
* `. D" y5 N+ V2 ~  s$ ?8 n; I   while (line != null) {
7 Q5 R* H1 D2 Z  d* P    String temp[] = line.split("[\\t\\s]+");
" A; z/ F( S: ?9 x$ H& N    line = in.readLine();1 w4 y% ~4 {6 H& _
    for (int j = 0; j < ySize; j++) {
1 S, f+ A& |; \/ Y; X; l( U' e( v     matrix[i][j] = temp[j];
( c! R5 l( O: Q9 F& Z- r    }
- y' z4 ?, s0 X  u    i++;
3 f* D5 [( O% h% H+ ~   }
+ R" w& d2 C# P: K/ f  d6 S   in.close();
4 w7 E" {  e: l+ [# m9 m  } catch (IOException ex) {
2 e5 B  J* V  @# p+ Q1 O6 P0 L   System.out.println("Error Reading file");1 E) u& D9 s$ Q% W) f+ i) p
   ex.printStackTrace();
( E% v6 [* B+ v0 V, ~1 r* H   System.exit(0);
, ?+ ~3 ~3 m" s* X2 @% |  }
* M7 @' h( @& w) W' [3 d- t/ p }
" A8 ^. I) R/ k' _ public String[][] getMatrix() {/ U9 q3 X" {0 {2 {) m. H
  return matrix;
, o1 X# N& {- D }
; l6 n1 c9 g( r. v3 G# u/ x}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-12 07:18 , Processed in 0.015358 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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