package 例题;
import java.io.File;
public class 例题1 {
public static void main(String[] args) {
//创建文件对象
File file = new File("D:\\Java15-1.docx");
//判断,如果该文件存在。exists存在的意思
if (file.exists()) {
//删除
//file.delete();
//System.out.println("文件已删除");
//获取文件名
String name = file.getName();
System.out.println("文件名叫:" + name);
//判断文件是否可写入
Boolean a = file.canWrite();
System.out.println("文件是否可写入:" + a);
}
//如果文件不存在
else {
//捕捉异常。不捕捉异常会报输入输出异常
try {
//创建文件
file.createNewFile();
System.out.println("文件已创建");
}
//抛出异常
catch (Exception e) {
e.printStackTrace();
}
}
}
}
package 例题;
import java.io.File;
public class 例题2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("c:\\happy\\word.txt"); //创建文件对象
if(file.exists()) {
String name=file.getName(); //获取文件名称
long length=file.length(); //获取文件长度
boolean hidden=file.isHidden(); //判断文件是否为隐藏文件
System.out.println("文件名称:"+name);
System.out.println("文件长度是:"+length);
System.out.println("该文件是隐藏文件吗?"+hidden);
}
else {//如果文件不存在
System.out.println("该文件不存在");
}
}
}
package 例题;
import java.io.File; //缓冲流 //字节流 FileOutputStream输出流 FileInputStream输入流
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 例题3 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("C:\\happy\\word.txt");
FileOutputStream out = new FileOutputStream(file); //输出字节流
byte[]by = "hello world".getBytes();
out.write(by); //往文件中写入数据
out.close();
FileInputStream in = new FileInputStream(file);
byte[] by1 = new byte[1024];
int len = in.read(by1);
System.out.println(new String(by1,0,len));//从零号位开始打印,只打印len长度
}
}
package 例题;
import java.io.BufferedReader; //文件流
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader; //字符流 FileWriter输出流 FileReader输入流
import java.io.FileWriter;
import java.io.IOException;
public class 例题4 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("C:\\happy\\word.txt");
FileWriter writer = new FileWriter(file); //输出字符流
//char[]by = new char[] {'h','e','l','l','o'}; //输入数据
//带缓存的输出流
BufferedWriter bw = new BufferedWriter(writer); //BufferedWriter缓存流
//String str="门前大桥下,游过一群鸭";
bw.write("门前大桥下,游过一群鸭");
bw.newLine(); //换行
bw.write("快来快来数一数,二四六七八");
bw.newLine();
bw.write("咕嘎咕嘎,真呀真多");
bw.newLine();
bw.write("数不清到底多少鸭");
bw.close();
//writer.write(str); //往文件中写入数据
writer.close();
FileReader reader = new FileReader(file);
/*char[] ch1 = new char[1024];
int len = reader.read(ch1);
System.out.println(new String(ch1,0,len));*/
BufferedReader br =new BufferedReader(reader);
String s1 = br.readLine();
System.out.println("第一行:"+ s1);
System.out.println("第二行:"+ br .readLine());
System.out.println("第三行:"+ br .readLine());
System.out.println("第四行:"+ br .readLine());
reader.close();
}
}
package 例题;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class 例题5 {
public static void main(String[] args) throws IOException {
//定义一个String类型数组
String conten[] = {"好久不见","最近好吗","常联系"};
//创建文件类对象
File file = new File("D:\\word.txt");
//文件字符输出流
FileWriter fw = new FileWriter(file);
//带缓存字符输出流
BufferedWriter bw = new BufferedWriter(fw);
//循环次数
for(int k = 0; k < conten.length; k++) {
//写入循环的次数
bw.write(conten[k]);
//换行
bw.newLine();
}
bw.close();
fw.close();
//文件字符输入流
FileReader fr = new FileReader(file);
//带缓存文件字符输入流
BufferedReader br = new BufferedReader(fr);
//定义String类型变量值为空
String tmp = null;
//定义int类型变量
int i = 1;
//while循环,条件是tmp不等于空进入循环
while ((tmp = br.readLine())!=null) {
System.out.println("第" + i + "行:" + tmp);
i++;
}
br.close();
fr.close();
}
}
package 例题;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 例题6 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("C:\\happy\\word.txt"); // 创建文件对象
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeBoolean(false);
dos.writeUTF("你好世界");
dos.writeDouble(13.25);
dos.close();
fos.close();
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream (fis);
System.out.println(dis.readBoolean());
System.out.println(dis.readUTF());//输出字符串
System.out.println(dis.readDouble());
dis.close();
fis.close();
}
}