JAVA 文件输出的问题
来源:百度知道 编辑:UC知道 时间:2024/05/11 01:34:15
import java.io.*;
class text
{
public static void main (String[]args)throws IOException
{
PrintWriter pw=new PrintWriter(new FileWriter("result.txt"));
pw.println("12345");
}
}
编绎能通过,可result文件是0字节,为什么?
怎么将想输出的写入文件
用下面的程序也是同样的问题:
import java.io.*;
class text
{
public static void main (String[]args)throws IOException
{
PrintWriter pw=new PrintWriter(new FileWriter("result.txt"));
pw.println("12345");
}
}
高手指点
class text
{
public static void main (String[]args)throws IOException
{
PrintWriter pw=new PrintWriter(new FileWriter("result.txt"));
pw.println("12345");
}
}
编绎能通过,可result文件是0字节,为什么?
怎么将想输出的写入文件
用下面的程序也是同样的问题:
import java.io.*;
class text
{
public static void main (String[]args)throws IOException
{
PrintWriter pw=new PrintWriter(new FileWriter("result.txt"));
pw.println("12345");
}
}
高手指点
加上pw.flush();pw.close();
File f = new File("result.txt");//产生文件
Long l = f.size();//文件大小
InputStream input = new FileInputStream(f);//产生一个输入流
BufferedReader buffer = new BufferedReader(new InputStreamReader(input)); //用来读取字符流,因为txt是文本文件
String value = buffer.readLine(); //读取这个文件第一行的内容
while(value != null){
System.out.println(value); //把读取的一行的内容输出
value = buffer.readLine(); //继续读
}
buffer.close();
input.close();
如果要读文本文件,基本是这样的。没有用java编辑器写,可能有错的。