设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5802|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;$ l) i1 A" `$ e: n
import java.io.BufferedReader;5 F* C3 b2 P# E7 X4 J5 h; g
import java.io.FileInputStream;* C$ c: A6 V! s* J6 T* p
import java.io.FileNotFoundException;
" G* a) {$ d/ ^9 U' Gimport java.io.IOException;" d/ A* N5 ]8 J; _, r
import java.io.InputStreamReader;; s: T" p. j& v$ z5 N; J+ _
import java.io.UnsupportedEncodingException;
1 M2 T! q# C" M1 L' Eimport java.util.StringTokenizer;8 d+ x1 x5 C2 I' H
public class TXTReader {
: {; x6 |6 d  q/ m5 j9 u3 l protected String matrix[][];
- I0 y" t/ C4 m  P' X2 Z protected int xSize;3 q$ Q* B- Z! O
protected int ySize;
6 }, p7 \1 f5 N. ~4 T% Z5 M$ k public TXTReader(String sugarFile) {* g* [9 t" U" P3 l- d1 C
  java.io.InputStream stream = null;* L( A- W  N. @! F' @" \
  try {
9 Y" s0 S& E# \: Q; V' o3 q; h   stream = new FileInputStream(sugarFile);: e; r7 u2 ~- r& B- {
  } catch (FileNotFoundException e) {
( T  Q9 I; s3 u0 ~: `   e.printStackTrace();# P3 o; A% `& r
  }
) s, c/ b- ?$ V# E$ A6 l) F6 @/ _0 x  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
8 A$ Y! @! N7 q4 @) f. E* o  init(in);
, F1 f$ K3 E& C0 [* e }
9 r8 S: f& F+ G& T4 J! h: l9 v6 @ private void init(BufferedReader in) {
' E; ^! F1 X1 Q' d  try {! x$ |( g' R4 n9 X! G2 m. a) {# u
   String str = in.readLine();0 c8 X! [6 Y  j/ N
   if (!str.equals("b2")) {
3 f) c' f* X7 `9 ]8 ~' y* V    throw new UnsupportedEncodingException(
) ^  @7 j# {( t6 ?8 L' A8 t      "File is not in TXT ascii format");; S% k5 R* K8 p9 f
   }
* \5 m/ T7 b1 w0 V5 O% k   str = in.readLine();
. d! V! X  |" u4 a& e   String tem[] = str.split("[\\t\\s]+");
- G3 v( F1 B& u* O1 J1 S   xSize = Integer.valueOf(tem[0]).intValue();
7 {" I: Z9 F6 q   ySize = Integer.valueOf(tem[1]).intValue();
$ @5 }  z& i: ]8 w   matrix = new String[xSize][ySize];
5 }7 o. W) _0 P- W* w! W8 O* |   int i = 0;
9 A/ F# d# t; @* T& ?9 g   str = "";
# I3 P: y# a9 c: Q+ k  \& U: @5 V   String line = in.readLine();# R" @  u- {5 l* a
   while (line != null) {  _- Z" E6 @2 r& D
    String temp[] = line.split("[\\t\\s]+");
, ?* v3 s$ x; e4 P* w% @8 v    line = in.readLine();
5 ?' f1 ?4 }  g) R    for (int j = 0; j < ySize; j++) {
, {" T5 ~: G) f: |! R# S& H0 Y     matrix[i][j] = temp[j];
# ~' R/ r  Q9 I0 i    }5 P6 k) T( v* M) o. S
    i++;
0 V0 w1 z2 Z$ Q( }   }' W* e! Q8 |" M. N4 q
   in.close();
2 j. w% m3 \$ J; W- Y+ r! l  } catch (IOException ex) {1 X, o" y1 V5 o7 ]
   System.out.println("Error Reading file");/ g4 y# M/ t: y% Z  i
   ex.printStackTrace();
& H7 O9 m$ j1 q   System.exit(0);
# K3 @$ f  _( o  }
! g  f# g0 g8 h8 @% n# E- Z% m }4 c  w) ?) S+ d5 S
public String[][] getMatrix() {6 Y$ Q: M1 P. R& k) I
  return matrix;$ |, c4 u  O9 \8 J
}
7 h- {1 k7 A* w# D. w}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-24 06:35 , Processed in 0.021175 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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