Topic: 关于读取整个文件字符串的问题,急,在线等

  Print this page

1.关于读取整个文件字符串的问题,急,在线等 Copy to clipboard
Posted by: mrs_cadbary
Posted on: 2005-12-02 08:55

我用readline读文本文件,总是遇到第一个换行符就停止了,用哪个方法可以读整个文件呢,而且希望读出来直接是字符串形式,因为我想用 split方法把这个文件分开。哪位高人指点我一下,谢谢了

2.Re:关于读取整个文件字符串的问题,急,在线等 [Re: mrs_cadbary] Copy to clipboard
Posted by: abc__abc1
Posted on: 2005-12-02 09:19

请具体说明一下,把代码放出来!

3.Re:关于读取整个文件字符串的问题,急,在线等 [Re: mrs_cadbary] Copy to clipboard
Posted by: mrs_cadbary
Posted on: 2005-12-02 09:36

比如说这一段,其实我并不是只想读取一行,而是想读取整个文件,然后把这个文件作为字符串以\r为分隔符用split切开。
BufferedReader p=new BufferedReader(new InputStreamReader(new FileInputStream("d:\\Jfiles\\printname2.txt"))); //读取文档
String a=p.readLine();
String[] aa = a.split("\r",0);

4.Re:关于读取整个文件字符串的问题,急,在线等 [Re: mrs_cadbary] Copy to clipboard
Posted by: mrs_cadbary
Posted on: 2005-12-02 09:41

或者循环使用readline把文件分成一个一个字符串也行,但是语句我就不会了

5.Re:关于读取整个文件字符串的问题,急,在线等 [Re: mrs_cadbary] Copy to clipboard
Posted by: abc__abc1
Posted on: 2005-12-02 09:50

printname2.txt文件上传给我

6.Re:关于读取整个文件字符串的问题,急,在线等 [Re: mrs_cadbary] Copy to clipboard
Posted by: abc__abc1
Posted on: 2005-12-02 10:05

不知道有没有帮助,仅供参考

import java.io.*;

public class ReadfileQ {

public ReadfileQ () {

try{

BufferedReader p=new BufferedReader(new InputStreamReader(new FileInputStream("data.txt"))); //读取文档
StringBuffer ab = new StringBuffer();
String a=null;
do{
a = p.readLine();
System.out.println("read data="+a);
if(a != null)
ab.append(a);
}while(a != null);
System.out.println(ab.toString());
}
catch(Exception e){

e.printStackTrace();
}
}

public static void main(String[] args) {

ReadfileQ rfq = new ReadfileQ();
}
}

7.Re:关于读取整个文件字符串的问题,急,在线等 [Re: mrs_cadbary] Copy to clipboard
Posted by: mrs_cadbary
Posted on: 2005-12-03 19:20

上面的循环挺有用,不过我要实现的是从一个名为printname的文件中查找ip地址为192.168.0.120的机器上程序运行的信息,我的java程序代码如下

import java.io.*;
import java.lang.*;
class Printname2
{ public static void main(String args[])throws IOException{ //@1 @2
BufferedReader p=new BufferedReader(new InputStreamReader(new FileInputStream("d:\\Jfiles\\printname2.txt"))); //读取文档
String a=p.readLine();
String[] aa = a.split("\\..",0); //以“#”为分隔符将文档分割为 b[]
//File print=new File("d:\\Jfiles\\print.txt"); //输出到文件
for (int i = 0 ; i <aa.length ; i++ )
{ String[] b=aa[i].split(" ",0); //以空格为分隔符将b[i]分割 @3
String string1=b[5];
String string2="submitted";
if(string1.equals(string2)==true) //当b[i]中包含submitted字段时执行以下程序
{ String[] c=b[8].split(":",0); //@4
String string3=c[0];
String string4="<192.168.0.120";
if (string3.equals(string4)==true) //当b[i]中包含要求的ip地址时执行以下程序
{String string5=b[1]; //@5
System.out.println(aa[i]); //输出此程序的请求信息
//PrintStream p1=new PrintStream(new FileOutputStream(print),true);
//p1.println(aa[i]);
for (int j = i+1 ; j <aa.length ; j++ )
{String[] d=aa[j].split(" ",0); //@6
String string6=d[1];
if (string6.equals(string5)==true) //以b[i]中第二字符串即进程号为筛选以下信息的关键字
{String string7=d[5]; //@7
String string8="executing";
if(string7.equals(string8)==true)
{System.out.println(aa[j]); //输出此程序的收到信息 @8
//PrintStream p2=new PrintStream(new FileOutputStream(print),true);
//p2.println(aa[j]);
for (int n = j+1 ; n <aa.length ; n++ )
{String[] e=aa[n].split(" ",0); //@9
String string9=e[1];
if (string9.equals(string5)==true)
{System.out.println(aa[n]); //输出此程序的执行信息 @10
//PrintStream p3=new PrintStream(new FileOutputStream(print),true);
//p3.println(aa[n]);
continue;
} //@10
} //@9
} //@8

} //@7
// else return;
} //@6

} //@5

} //@4
// else return;

} //@3
} //@2
}

如果我用readline的循环该怎么返回ip地址呢

printname.txt的内容如下(包含换行符)

000 (277.000.000) 11/04 20:51:40 Job submitted from host: <192.168.0.121:1027>
..
001 (277.000.000) 11/04 20:51:44 Job executing on host: <192.168.0.121:1026>
..
005 (277.000.000) 11/04 20:51:45 Job terminated.
(1) Normal termination (return value 0)
Usr 0 00:00:00, Sys 0 00:00:00 - Run Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Run Local Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Local Usage
203 - Run Bytes Sent By Job
96 - Run Bytes Received By Job
203 - Total Bytes Sent By Job
96 - Total Bytes Received By Job
..
000 (278.000.000) 11/04 20:53:12 Job submitted from host: <192.168.0.120:1027>
..
000 (279.000.000) 11/04 20:53:12 Job submitted from host: <192.168.0.120:1027>
..
000 (280.000.000) 11/04 20:53:13 Job submitted from host: <192.168.0.121:1027>
..
001 (278.000.000) 11/04 20:53:16 Job executing on host: <192.168.0.120:1026>
..
005 (278.000.000) 11/04 20:53:16 Job terminated.
(1) Normal termination (return value 0)
Usr 0 00:00:00, Sys 0 00:00:00 - Run Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Run Local Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Local Usage
203 - Run Bytes Sent By Job
96 - Run Bytes Received By Job
203 - Total Bytes Sent By Job
96 - Total Bytes Received By Job
..
001 (279.000.000) 11/04 20:53:19 Job executing on host: <192.168.0.120:1026>
..
005 (279.000.000) 11/04 20:53:19 Job terminated.
(1) Normal termination (return value 0)
Usr 0 00:00:00, Sys 0 00:00:00 - Run Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Run Local Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Local Usage
203 - Run Bytes Sent By Job
96 - Run Bytes Received By Job
203 - Total Bytes Sent By Job
96 - Total Bytes Received By Job
..
001 (280.000.000) 11/04 20:53:21 Job executing on host: <192.168.0.121:1026>
..
005 (280.000.000) 11/04 20:53:21 Job terminated.
(1) Normal termination (return value 0)
Usr 0 00:00:00, Sys 0 00:00:00 - Run Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Run Local Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Remote Usage
Usr 0 00:00:00, Sys 0 00:00:00 - Total Local Usage
203 - Run Bytes Sent By Job
96 - Run Bytes Received By Job
203 - Total Bytes Sent By Job
96 - Total Bytes Received By Job
..


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923