设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6080|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
( X. L+ P; ]% Gimport java.io.BufferedReader;
0 O# T9 S! F& ^import java.io.FileInputStream;- k8 |, u* R! I9 m& e
import java.io.FileNotFoundException;
! t7 C0 D* j2 x# m) H- Qimport java.io.IOException;
, R* m2 d) W0 H! W  Ximport java.io.InputStreamReader;
. u9 x# t- l( F( W0 fimport java.io.UnsupportedEncodingException;# l/ W( L$ F1 j
import java.util.StringTokenizer;. O7 a, U4 k! L# S
public class TXTReader {
7 g0 B0 \; g& `. v  g$ M. M2 L0 _* R protected String matrix[][];
  U" `* J+ E3 |1 _ protected int xSize;5 H/ ?/ b$ k7 t3 T
protected int ySize;6 e- i. k+ X7 t3 f9 s5 j
public TXTReader(String sugarFile) {
5 t8 _4 Z& c, a, i% u# d0 k2 x  java.io.InputStream stream = null;3 R2 r  o. U- U0 L0 O
  try {
0 |4 O/ c) T- s+ G* l; A   stream = new FileInputStream(sugarFile);4 f+ X: p" E+ H6 c, ~
  } catch (FileNotFoundException e) {
, m& S5 I4 P3 w1 g) r4 F4 }   e.printStackTrace();
" [4 ^% C& w, j3 w  }+ |; X# T* w: x( j* t
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));+ q$ P+ R# O" N; u& E* s
  init(in);' [) ^. [8 z8 j# q' i4 a9 v/ N
}
6 \, J! E; m: z  M# T0 Y1 c private void init(BufferedReader in) {5 Z  x- F" r& n! @
  try {
1 l; }: X, Z$ o6 y" X   String str = in.readLine();
9 `! U! x, V! D: p/ Q   if (!str.equals("b2")) {, G5 W3 N1 @2 B9 j1 j3 j" p
    throw new UnsupportedEncodingException(
) z- H; c8 u8 ~- V      "File is not in TXT ascii format");
! F+ ]9 W! }  c8 n/ `/ w' m: I   }
5 C* j; j0 s( Q6 c, l& v   str = in.readLine();
! f4 q. z) f5 |- O! w8 z% h   String tem[] = str.split("[\\t\\s]+");! v( N7 I  x7 Q1 h
   xSize = Integer.valueOf(tem[0]).intValue();% B" N% p3 K* ?$ n4 X( D, q
   ySize = Integer.valueOf(tem[1]).intValue();
. e7 `- X, o  j7 F, p8 E7 x   matrix = new String[xSize][ySize];
9 _$ D1 @- F% ]6 Z: K2 T# Q# o8 _   int i = 0;
  M# k+ L) ^4 a/ d- @' {( w   str = "";
/ ^" D  h4 w" g1 c& B# o   String line = in.readLine();
9 f4 s% R8 Q1 e' z( m  ?   while (line != null) {
- G' e6 r. _! {8 y" j6 A" @7 P    String temp[] = line.split("[\\t\\s]+");; t8 t# o$ \8 H, u3 l$ L! n0 c
    line = in.readLine();
* g- ^" l8 J: z! d2 A' Q( S    for (int j = 0; j < ySize; j++) {
( l  M6 |9 B# b( `) I* l; j# s* q" X     matrix[i][j] = temp[j];
4 k4 s7 [  c. U# v8 Z    }  V; x( A) W, c
    i++;+ m; E( @9 ?% a/ q
   }: s8 I8 z" u) W0 J
   in.close();
7 h( J% O6 t$ f  } catch (IOException ex) {
2 @8 V6 W& {1 g& G   System.out.println("Error Reading file");9 L' t: V+ r, R6 c4 v
   ex.printStackTrace();
- K9 E' s2 s$ U: Q* [   System.exit(0);! `2 C: z9 D9 F
  }  o2 B1 M2 H$ L. O
}' g, ]- @! L, K7 \; C' @
public String[][] getMatrix() {
  `* n$ O3 p! U! X  m0 ^8 j0 p5 S  return matrix;; W$ b0 a8 D  `% J: W
}
8 Z; p1 S3 j/ ?}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-10 10:55 , Processed in 0.019686 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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