当前位置: 技术问答>java相关
请帮忙看一下好吗?应该很简单的,只是我搞不定。能够编译,不能运行。
来源: 互联网 发布时间:2015-07-13
本文导语: **读取文件 /*************************ReadFile.java********************/ import java.io.*; public class ReadFile { public static void main(String args[]) throws Exception { BufferedReader br=new BufferedReader( new FileReader( "1.txt" )); String s; while (b...
**读取文件
/*************************ReadFile.java********************/
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(
new FileReader( "1.txt" ));
String s;
while (br.ready());
{
s = br.readLine();
System.out.println("your sentence is:" + s);
}
}
}
/*************************ReadFile.java********************/
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(
new FileReader( "1.txt" ));
String s;
while (br.ready());
{
s = br.readLine();
System.out.println("your sentence is:" + s);
}
}
}
|
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(
new FileReader( "g://app.log" ));
String s;
int n=br.read();
while(n!=-1)
{
System.out.print((char)n);
n=br.read();
if(n==13)//判断是否为断行字符
{
System.out.print("/r");
br.skip(1);
n=br.read();
}
}
br.close();
}
}
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(
new FileReader( "g://app.log" ));
String s;
int n=br.read();
while(n!=-1)
{
System.out.print((char)n);
n=br.read();
if(n==13)//判断是否为断行字符
{
System.out.print("/r");
br.skip(1);
n=br.read();
}
}
br.close();
}
}
|
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(new FileReader("1.txt"));
String s;
if (br.ready())
{
s = br.readLine();
System.out.println("your sentence is:" + s);
}
}
}
用if判断文件是否ready,用while判断文件是否eof
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(new FileReader("1.txt"));
String s;
if (br.ready())
{
s = br.readLine();
System.out.println("your sentence is:" + s);
}
}
}
用if判断文件是否ready,用while判断文件是否eof
|
**读取文件
/*************************ReadFile.java********************/
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(
new FileReader( "1.txt" ));
String s;
while (br.ready()) //这里的分号去掉!!!
{
s = br.readLine();
System.out.println("your sentence is:" + s);
}
}
}
/*************************ReadFile.java********************/
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(
new FileReader( "1.txt" ));
String s;
while (br.ready()) //这里的分号去掉!!!
{
s = br.readLine();
System.out.println("your sentence is:" + s);
}
}
}
|
while (br.ready());
~~~~~~~~~~~~~~~~~~~~你有分号?这里死循环了
~~~~~~~~~~~~~~~~~~~~你有分号?这里死循环了
|
br.ready() 是用来判断文件是否已经打开了,你已经打开后就会加锁,
第二次就不能在打开了。
第二次就不能在打开了。
|
用if判断文件是否ready,用while判断文件是否eof