设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8848|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;  z8 ^, b- n* V' H) b& X5 _9 U
import java.io.BufferedReader;
  h- N- v: }1 h+ F. v4 Wimport java.io.FileInputStream;! a# B* T! y2 F9 S' T( ^& }- O3 N  C" F
import java.io.FileNotFoundException;3 i# S8 y" n# y
import java.io.IOException;
4 [  p$ N* j5 D- ]2 Q( o) nimport java.io.InputStreamReader;
+ @6 _) c7 H9 a. w' W# n5 ~, kimport java.io.UnsupportedEncodingException;
! q2 p# W# ^# {6 ?4 k0 fimport java.util.StringTokenizer;
. s# k4 r" L6 y$ ?public class TXTReader {
; }+ z' _7 i6 V+ M9 a protected String matrix[][];; \  Y$ x/ k, v. J
protected int xSize;! c" F2 A1 j2 ]
protected int ySize;
' d9 U7 q0 _" o" @ public TXTReader(String sugarFile) {: n. ?$ v$ ?' H) n
  java.io.InputStream stream = null;' z, ~  [0 u$ s2 v* U9 i, y! M
  try {/ Z6 n' |% I  @" s7 _" t$ B. D
   stream = new FileInputStream(sugarFile);3 ?, t8 i, J4 `0 i
  } catch (FileNotFoundException e) {
- ^8 J# s5 d) x; G5 B   e.printStackTrace();  M  Y6 ~( p, \& Z. c7 A
  }# C7 F# C6 D' B" l  O& q* }
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));: ]( h& K6 Z% X+ e# q
  init(in);
+ X) i& s& B2 L  M }0 R0 s) `. Q* k/ v& A& |
private void init(BufferedReader in) {. n* _  \$ I/ f2 d  e
  try {# O- J, m4 r9 W& }! i& W* |3 V
   String str = in.readLine();
( A' r; o/ D" R/ r5 K2 p   if (!str.equals("b2")) {
1 M% k4 k. ]+ e    throw new UnsupportedEncodingException(
+ Z% @* k& L4 [      "File is not in TXT ascii format");
6 e. {# X7 [( F- x" K8 a( N# P   }) v9 t% y" B2 m% H9 f- e+ a
   str = in.readLine();7 D9 a& D8 L) p9 E, X
   String tem[] = str.split("[\\t\\s]+");
! g6 C# E4 t/ [   xSize = Integer.valueOf(tem[0]).intValue();
# x, `$ y  m* v7 o- t  \   ySize = Integer.valueOf(tem[1]).intValue();6 B6 b) k1 e! v3 r9 E
   matrix = new String[xSize][ySize];/ H: e$ |7 i/ U$ k" {
   int i = 0;* P" q" i' r2 w5 ?! R4 N$ U( _
   str = "";
2 u" o4 U5 f/ W9 O2 i0 a6 B   String line = in.readLine();2 _! E1 [, T* W! I6 @. b8 @
   while (line != null) {
: j! |, E8 ^% z3 ]9 w    String temp[] = line.split("[\\t\\s]+");, s" f- `% Z) T) O
    line = in.readLine();$ N, g  P. `) R2 \" O
    for (int j = 0; j < ySize; j++) {
5 R! b; p& h$ w+ c0 v* {$ Z3 I; z% e     matrix[i][j] = temp[j];
; f7 m# r1 r. d+ _! Q7 l& Y    }9 V" H- S8 h$ [0 K) U; Z
    i++;
9 W* X% O! X' h9 G# Y. @3 I0 k   }4 ?8 |* K" U1 W, ?/ r1 ]4 |5 Z' e
   in.close();
$ }* x& P" H8 ^  } catch (IOException ex) {
+ e# i) p# m! i& i8 o. Y   System.out.println("Error Reading file");
( y9 K% F- Q; {% a. H0 J: b! i/ l  J. {   ex.printStackTrace();
% Y6 ~5 q% ]; r1 G8 q3 I8 f   System.exit(0);
  D! Z! T- _0 H) A6 {  }- Q+ q7 ?$ o8 T; w
}6 k- `: d2 F' e0 O8 t. J9 Z
public String[][] getMatrix() {8 L$ ^+ z8 I1 J! W8 p' O2 w& ~
  return matrix;
, ]( U# g" O; ^- L }; u/ Y7 l2 `( l8 M% f0 m  t
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-17 05:50 , Processed in 0.015914 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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