设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10362|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;6 H+ g# z+ p/ y5 P$ j$ W( H
import java.io.BufferedReader;
. f% k% g+ M  \$ }' k1 Jimport java.io.FileInputStream;
. f" R5 |  e6 A. n) Q! {import java.io.FileNotFoundException;( d- q! L+ b$ a" E
import java.io.IOException;
1 H- o! Z  }7 b+ g. l6 qimport java.io.InputStreamReader;" p* I& u( }  l1 o9 {( a& M
import java.io.UnsupportedEncodingException;' V6 \; [' X( w, q, ]* {; T
import java.util.StringTokenizer;
0 @3 g( |. D* C6 l- S5 p# W/ c- tpublic class TXTReader {
: i% y; k6 ~) [0 S, C1 `" f# P$ {+ R protected String matrix[][];% O4 N9 h: Y& v7 o, S4 j+ [
protected int xSize;
: U  R2 X5 ]: R0 ~( U protected int ySize;1 r; r; r7 ]9 C
public TXTReader(String sugarFile) {+ A1 u5 X! Z) |" g) O( v# M5 l
  java.io.InputStream stream = null;
+ _' B/ G! C4 x# ^! \  try {
- h9 X) t) a/ N7 }3 R* f! c   stream = new FileInputStream(sugarFile);9 |* g2 F* p8 P3 E9 D
  } catch (FileNotFoundException e) {
6 n; W5 D8 }- T# ?" x6 e; P   e.printStackTrace();
/ L: r2 f4 I$ M6 ~$ u& B  }
5 U0 ]( ~7 R/ Z7 g- M  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
4 J! p) @, X; ^8 v/ W' M  init(in);$ @0 x+ Z( C0 J  j" n8 d
}. t& ~, K9 Q+ n' ?- u
private void init(BufferedReader in) {/ X3 C. U7 I, E: `6 k
  try {7 [7 N/ A3 T! _$ [1 t" S
   String str = in.readLine();
, @& N. q8 b7 Z7 ^5 N" H1 R- t   if (!str.equals("b2")) {
7 L1 S* A% t% X. r  G    throw new UnsupportedEncodingException(
# V% }" {6 Y( |9 r! r; v      "File is not in TXT ascii format");
# L; j- |5 P5 u* k* B9 W9 \   }
. b5 b( T; {  d   str = in.readLine();3 }& I( _! x! J2 [& n& ~
   String tem[] = str.split("[\\t\\s]+");1 ^0 [' ]' e2 ^, l
   xSize = Integer.valueOf(tem[0]).intValue();
% H  b* U) s3 D4 Z; ]; x8 g* d" `5 Y   ySize = Integer.valueOf(tem[1]).intValue();$ j; m* C! N9 u# y8 @2 V
   matrix = new String[xSize][ySize];
7 R, s  P$ Y* B6 w% M4 @; ~   int i = 0;; a0 K4 m# ^. \
   str = "";
% N4 h; G+ r5 L9 U, [& A. l   String line = in.readLine();" k' I( \, Z. u" d
   while (line != null) {
6 y4 S% \6 U3 N- C    String temp[] = line.split("[\\t\\s]+");
! b/ i7 S: J- E+ A0 V    line = in.readLine();1 |) B9 r( {# ]; w; K  H. C/ I
    for (int j = 0; j < ySize; j++) {# {( s4 z' }7 |3 R2 h" B5 d/ P  t
     matrix[i][j] = temp[j];9 t1 P  t! F7 A5 a, t
    }
) D9 N1 w  d& }: U2 a9 O3 y+ K    i++;
( f+ N: }# I# z" t  |   }
' m" e- i" m! Z' }  I   in.close();
+ b# @$ S+ o6 w0 M0 n( k) Q  } catch (IOException ex) {2 `' [8 q2 B7 A: _
   System.out.println("Error Reading file");
% S1 p5 C0 B6 \5 h! [* ]* W   ex.printStackTrace();7 f: c! i! c# w
   System.exit(0);4 n8 {! Y) O) ^  j) |# e
  }, W& r% Y# m# l3 g
}* R7 \3 n. q( i; }% s& m' I9 d
public String[][] getMatrix() {
4 ~+ ?, W- w1 T  return matrix;
+ P4 {& ~* I# ?$ b }
2 s8 m- d/ i* p. a) k}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-6 05:52 , Processed in 0.013925 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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