设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6065|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
& l. t% y, J& q- _% rimport java.io.BufferedReader;
; I6 H3 p, J( d' c, Y' G0 Himport java.io.FileInputStream;
# _% D5 e* C; Gimport java.io.FileNotFoundException;" J5 g$ x# O# R
import java.io.IOException;
1 t# e" S9 ]0 x% V+ p  Limport java.io.InputStreamReader;& T2 S9 H4 a& w
import java.io.UnsupportedEncodingException;7 z; K& V, z5 S, N4 f2 `) H
import java.util.StringTokenizer;
" |; }7 F' `- [public class TXTReader {
' }# z) N- f3 v" `1 d& t protected String matrix[][];
, ~% Q- Z8 V( M8 ^5 G9 i# [: @ protected int xSize;. |% x4 ?3 Z9 c& S5 V$ y' e
protected int ySize;
; D5 c* ^6 j5 m6 _" h8 D9 g0 Q/ h9 O0 K public TXTReader(String sugarFile) {
0 x- J4 |2 t6 X2 N# R% O  java.io.InputStream stream = null;' _+ d/ ?( }# o+ ^. h9 I* ]
  try {
( r2 @  [- _3 ^/ J: N& g! R! Z  T( o   stream = new FileInputStream(sugarFile);
8 W4 U8 i" \! V% |  } catch (FileNotFoundException e) {) j: N" A( e; d) e- S
   e.printStackTrace();1 T- u' Z! X7 O% q+ ?' v. Y9 E
  }7 ^$ e4 Q* g# Q" W  O# K
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
  b( {; d" h9 t1 e3 m' D2 q  init(in);
1 Q! Q. G7 \/ X% C& ?' o3 L: b2 o }
% \% ^0 ~1 \' [% b* M1 P+ E  ] private void init(BufferedReader in) {' O  I2 l2 k* u- l
  try {) P2 O- F2 J7 h- y. u
   String str = in.readLine();
5 N% H+ I, b2 o) I0 n3 M" Z* B7 G   if (!str.equals("b2")) {
' B7 X' H3 `6 H0 W' w/ v) `) a- ?    throw new UnsupportedEncodingException(
, b+ l' d" G# T, j8 c      "File is not in TXT ascii format");
: c8 x3 S6 V. G+ U- X# @   }
9 Z5 |- t+ f/ N' T6 }" [, W   str = in.readLine();
" E( J( V" D! p1 H  e   String tem[] = str.split("[\\t\\s]+");! C2 d/ e9 r' X
   xSize = Integer.valueOf(tem[0]).intValue();
) z, b9 t2 K; @4 K( W" y1 J   ySize = Integer.valueOf(tem[1]).intValue();# \8 E$ X6 h( D+ F- Z
   matrix = new String[xSize][ySize];
  l6 _$ o7 K: }- q1 z+ w   int i = 0;( B( q: G( F0 C$ H% e0 s# F3 v5 N
   str = "";' {( k3 d. G& O& q+ [) ~: a+ P! E
   String line = in.readLine();
7 `2 ^: g  `9 E( @% {" j/ O0 s1 B8 B: h   while (line != null) {: X% l. G+ k, Q5 j7 r
    String temp[] = line.split("[\\t\\s]+");
1 m$ S! j9 m7 i. V3 K" Y: T    line = in.readLine();/ K7 e8 ?! R! j6 [
    for (int j = 0; j < ySize; j++) {6 n$ }1 m" w5 ~& a& m' c" `; i, I
     matrix[i][j] = temp[j];
/ G( u. z& M6 F' K$ t9 `    }
( p# g) [, `' V; }- i* I5 {' N$ J    i++;- z$ f! T/ E$ e8 m5 X% a
   }; v8 [& k  l, \- _/ V2 ~9 ]8 W
   in.close();
* H' P& f3 L0 N) N: A  } catch (IOException ex) {9 I; y' J* H$ ?- B
   System.out.println("Error Reading file");# l- `. U/ R; z1 d* U
   ex.printStackTrace();9 R+ e, ?; ~! u. `- B+ {
   System.exit(0);
  T2 `+ U! y" `  }
5 q& W. y, n- [# W! H }  t- ]3 T; Y' ]0 x
public String[][] getMatrix() {
! c; Q- R: N2 }- x0 l  return matrix;
3 c! e" |/ a' R4 c# I0 d8 ^5 s }
' k4 c- x8 }7 T4 H! |}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-9 14:27 , Processed in 0.024617 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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