设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6426|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
# r& B& O: U- u4 Z4 t& S0 E! cimport java.io.BufferedReader;
6 O  z" J* m# F7 b' a$ Uimport java.io.FileInputStream;& Q. D3 S, N& {$ Y) o- A! _
import java.io.FileNotFoundException;
1 l( P# {. n' V7 h$ q7 _% K2 limport java.io.IOException;$ C* ]! u/ ]4 V8 K1 N& S0 O
import java.io.InputStreamReader;, j' K* Y3 W- b! A, U# A
import java.io.UnsupportedEncodingException;9 V0 c% D- C5 S! j5 k! z
import java.util.StringTokenizer;
( I' a; W' L0 z$ s0 qpublic class TXTReader {
7 M& ^. |, y. o$ x) n2 N" K6 ? protected String matrix[][];: k0 Z7 U0 i7 x- f! r0 V
protected int xSize;( k2 b7 ^6 d; W0 \
protected int ySize;% a& W' j, o  p! c$ O/ I: z3 A
public TXTReader(String sugarFile) {5 E. A+ E  ^/ G4 x
  java.io.InputStream stream = null;
  V9 k) k( l  G* Q% E! m  try {$ A& F9 ?6 {; u8 ~
   stream = new FileInputStream(sugarFile);
5 b+ v5 n4 s$ Z8 A  } catch (FileNotFoundException e) {
4 Q* q# l3 a, C# A   e.printStackTrace();) [* \/ R4 v! \% ]& i4 A
  }
1 M% D) V; H- x% ^7 P+ k9 x* N7 \  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
3 U5 W" A2 c$ {& g/ M2 ]- X# ]  init(in);
: g' |0 }0 A8 o2 c& Q  X }/ W. Y' h0 x( E, ^+ q! S
private void init(BufferedReader in) {
4 |! o; F5 V) q# \( N  try {
# r7 W8 T! c; u* O2 w" a# Q) f   String str = in.readLine();) m! o( n: j* D( r" l
   if (!str.equals("b2")) {
& @! G" }5 x! D2 E4 w( M    throw new UnsupportedEncodingException(
/ i8 U% K! c( ^( J      "File is not in TXT ascii format");8 j3 f9 E7 w% O, v; A6 ~
   }) m2 b* T7 i  T  i' o  u
   str = in.readLine();1 o9 D2 x1 S) ^  F) _2 u" z
   String tem[] = str.split("[\\t\\s]+");
7 D9 G- Y9 Z& \+ t   xSize = Integer.valueOf(tem[0]).intValue();
2 a7 ]- ]9 E5 Y- b: }   ySize = Integer.valueOf(tem[1]).intValue();) {% \- t: Z+ E5 h0 a  n5 m) X% g) k
   matrix = new String[xSize][ySize];
+ K  }: l7 A3 a. q   int i = 0;3 q( x; c& l) a4 \* k. ^
   str = "";
8 R4 V7 }0 a! `; o% w) l1 r   String line = in.readLine();. ?6 Y. j4 T# e2 }7 W" n
   while (line != null) {- E) _; S9 {+ E+ J
    String temp[] = line.split("[\\t\\s]+");
# C0 ?, V1 @6 c8 v  m7 ~    line = in.readLine();
% n8 E& @6 K; W7 @    for (int j = 0; j < ySize; j++) {
* ~+ B" F8 T) l: q% g. D2 h3 ~     matrix[i][j] = temp[j];
  }7 G, b- [; E$ Q9 C" p* X7 Z, A' f    }+ \( o; t& A  T$ f
    i++;4 g# Q, D2 Q1 ^! `
   }  ]7 v) T! C( H
   in.close();
/ @! V. ]# }- R! t: l' D0 G  } catch (IOException ex) {7 D$ J3 d* j  p3 F8 P0 W
   System.out.println("Error Reading file");$ Y' S% T/ `% x9 C( P/ e$ V1 @
   ex.printStackTrace();
7 T2 L. V5 A: s6 O! F( h* b   System.exit(0);% d( h9 P: @  Q2 I
  }6 G2 R+ w! E6 N: |- M( X
}
" t' `/ T5 P) ^3 n! } public String[][] getMatrix() {
7 r* }3 Z; Y0 x& d3 \4 Q  return matrix;
4 z' J4 z# w# t8 J4 r6 R" h }: Z$ _+ J$ Y0 W. o: p$ [
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-10 05:36 , Processed in 0.020130 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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