1,文件的创建与删除
package Ioliu;
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args){
File file = new File("word.txt");
if(file.exists()){
file.delete();
System.out.println("文件已删除");
}else{
try {
file.createNewFile();
System.out.println("文件已创建");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
文件已创建2.获取文件信息
package Ioliu;
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args){
File file = new File("word.txt");
if(file.exists()){
String name = file.getName();
long length = file.length();
boolean hidden = file.isHidden();
String path = file.getAbsolutePath();
System.out.println("文件名称:" + name);
System.out.println("文件长度是: " + length);
System.out.println("该文件是隐藏文件吗?" + hidden);
System.out.println("文件的绝对路径:" + path);
}else{
System.out.println("该文件不存在");
}
}
}
文件名称:word.txt
文件长度是: 0
该文件是隐藏文件吗?false
文件的绝对路径:C:\Users\lenovo\workspace\Learn\word.txt
2:文件输入/输出流
FileInputStream类 , FileOutputStream 类 ,//字节输出,写入流
FileReader 类,FileWriter 类//字符输出,写入流
package Ioliu;
import java.io.*;
public class FileStreamTest {
public static void main(String[] args){
File file = new File("word.txt");
try {
FileOutputStream out = new FileOutputStream(file);
byte buy[] = "你好!".getBytes();
out.write(buy);//将数组中的信息写入到文件中
out.close();//将流关闭
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileInputStream in = new FileInputStream(file);
byte byt[] = new byte[1024];
int len = in.read(byt);//从文件中读取信息
System.out.println("文件中的信息是:" + new String(byt,0,len));
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileWriter out = new FileWriter(file);
String s = "你好啊!";
out.write(s);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
FileReader in = new FileReader(file);
char byt[] = new char[1024];
int len = in.read(byt);
System.out.println("文件中的信息:" + new String(byt,0,len));
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
3.带缓存的输入输出流
BufferedInputStream类 与 BufferedOutputStream类,它们与OutputStream InputStream 的区别是flush方法。
文件 ----> InputStream---->BufferedInputStream----->目的地
BufferedReader 类 与 BuffersdWriter类继承Reader类与writer类,可以以行为单位进行输入输出
字符数据----->BufferedWriter------>OutputStreamWriter------>OutputStream---->文件
package Ioliu;
import java.io.*;
public class Student {
public static void main(String args[]){
String content[] = {"好久不见","最近好吗","常联系"};
File file = new File("word.txt");
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bufw = new BufferedWriter(fw);
for(int k = 0; k < content.length; k++){
bufw.write(content[k]);//将字符串数组中的元素写入到磁盘文件中
bufw.newLine();//将数组中的单个元素以单行的形式写入文件
}
bufw.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileReader fr = new FileReader(file);
BufferedReader bufr = new BufferedReader(fr);
String s= null;
int i = 0;
while((s = bufr.readLine())!=null){
i++;
System .out.println("第" + i + "行:" + s);
}
bufr.close();
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}