设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8535|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;- f! ]' N0 \$ H$ `) C$ j% a
import java.io.BufferedReader;8 F3 }# }3 x* S, j, o  s
import java.io.FileInputStream;! @5 b4 R& r7 B" ^0 |0 `
import java.io.FileNotFoundException;' {, U  I- U! O
import java.io.IOException;
% O8 G+ \  Y2 A' q1 A/ Oimport java.io.InputStreamReader;
) z' a1 ^' }( ?" B1 u8 S! F: t" Ximport java.io.UnsupportedEncodingException;
& D1 k) n* L- |* V% R: |3 cimport java.util.StringTokenizer;
9 A& K/ e! y+ d% Xpublic class TXTReader {5 I' T. k0 T# f) c* ?2 q; X* C
protected String matrix[][];/ N' k# V) C! O, a$ N
protected int xSize;* n6 W7 a/ V6 d9 W( M
protected int ySize;4 G) O; |# H- A/ F" R
public TXTReader(String sugarFile) {
7 [+ a" S4 `: @0 T( P- J( F  java.io.InputStream stream = null;9 x9 Q; j. ]2 ~2 z: y
  try {
! C1 S3 h# c. y, W4 i: F   stream = new FileInputStream(sugarFile);- g; C; R! _  _  K) v
  } catch (FileNotFoundException e) {' \& l6 T& w# d( B% E  a
   e.printStackTrace();9 r- S/ x! a* E3 i9 l1 _
  }
8 C& g) Y# G' o% m+ {% p" N  D3 K5 a/ ^  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
/ C$ v: K9 j/ x  init(in);, u+ R5 z# P: D; E( P" c) p# ^
}
$ l1 F2 E7 L; x" F' C private void init(BufferedReader in) {
* J5 @" x8 M7 H/ `  W" b  try {
5 L" p4 _6 P& ?% z9 x' g! N9 k   String str = in.readLine();, v0 S/ Q4 y( {1 H
   if (!str.equals("b2")) {
7 Q5 F; J# D1 x( O: J' ~, N    throw new UnsupportedEncodingException(
3 K% A$ i: z5 p, h3 q) O& ^' z( P0 u      "File is not in TXT ascii format");
* E" n# G$ s6 n2 N% m: x   }: W* u$ v8 c; C# A
   str = in.readLine();
* m6 E: T& E& ]2 P" i, ?6 j5 p1 H! E   String tem[] = str.split("[\\t\\s]+");, P: b( I9 t0 k$ g# i$ @0 Z
   xSize = Integer.valueOf(tem[0]).intValue();5 e: e4 B6 y! T' w- a! _' E8 T
   ySize = Integer.valueOf(tem[1]).intValue();
& ]+ V9 l( e0 H$ n: e7 v. @   matrix = new String[xSize][ySize];
/ \, f3 T7 e$ ], f5 o* Y   int i = 0;8 k4 Y2 m+ j: [; q8 ~- d
   str = "";* D9 t$ `# I4 l9 U8 X
   String line = in.readLine();5 f4 l5 x( o2 y+ f
   while (line != null) {; ~1 \" ~5 K, R3 j0 n  W
    String temp[] = line.split("[\\t\\s]+");
: h# A3 K5 O* r/ Y; g4 I) u$ |% q/ _    line = in.readLine();: }# f  I' y5 u. _" N
    for (int j = 0; j < ySize; j++) {) K' Z; R. T4 H6 m1 h5 _  R5 U
     matrix[i][j] = temp[j];
: }% V; u! S7 {, x$ D/ R    }; @2 o9 Y. i+ i, a& f
    i++;% A4 G' h; V0 E! J
   }0 C8 h5 S. U# O
   in.close();$ y3 {, ^. x1 `+ C7 q# B
  } catch (IOException ex) {! N" l" e" _0 w- b, f+ _# \
   System.out.println("Error Reading file");
% h3 D+ f3 W" v9 l, ]   ex.printStackTrace();
+ L/ |  L; z4 E* D" n. ~- |; @9 h   System.exit(0);  P/ B; `# `0 S$ ?8 q6 O6 x0 ~
  }
0 j8 o8 K: B# U2 m0 D5 \ }
5 Y0 c: L$ J8 t0 Z! r# E* y public String[][] getMatrix() {0 I7 ]6 O9 j9 D% P
  return matrix;, X/ }* i% ~2 A0 j7 K
}% Z7 _4 R0 @7 s
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-28 21:06 , Processed in 0.016248 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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