现在就是在指定路径下有个文件,例如E:/csdn.txt这个文件,这个文件里面有内容
现在就是想对文件的内容做操作,首先我想读取E:/csdn.txt里的内容怎么读啊,还有如果我想把E:/csdn.txt的内容都删除了,让他的容量为0字节,怎么删啊,还有如果我想把一个新的内容写到E:/csdn.txt里,并且把以前的内容给覆盖掉,怎么做啊?
求java源代码,谢谢大虾门,我是初学人
现在就是想对文件的内容做操作,首先我想读取E:/csdn.txt里的内容怎么读啊,还有如果我想把E:/csdn.txt的内容都删除了,让他的容量为0字节,怎么删啊,还有如果我想把一个新的内容写到E:/csdn.txt里,并且把以前的内容给覆盖掉,怎么做啊?
求java源代码,谢谢大虾门,我是初学人
import java.io.*;
public class DisplayFile{
public static main(String[] arg) throws Exception{
BufferedReader 自定义变量名1=
new BufferedReader(
new InputStreamReader(
new FileInputStream("文件名")));
String 自定义变量名;
自定义变量名2=自定义变量名1.readLine();
System.out.pringtln(自定义变量名2)
}
}
文件需和程序在同一目录下,一次输出文件的一行,要输出多行可用循环
import java.io.*;
public class WriteFile{
public static void main(String[] args){
File file=new File("E:/csdn.txt");
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(file);
String s=in.readLine();
while(!s.equals(""))
{
out.println(s);
s=in.readLine();
}
in.close();
out.close();
}catch(IOException e){
System.out.println(e);
}
}
}
下面是读文件的.
import java.io.*;
public class ReadFile{
public static void main(String[] args){
try{
BufferedReader br=new BufferedReader(new FileReader("E:/csdn.txt"));
String s;
int i=0;
while((s=br.readLine())!=null)
{
System.out.println(++i+":"+s);
}
br.close();
}catch(FileNotFoundException e1){
System.err.print("File not found!");
}catch(IOException e2){
System.out.print(e2);
}
}
}
实现了各种IO操作的实现类,源码可以从以下网址下载。
http://commons.apache.org/downloads/download_io.cgi