io流
public void created1() {
String filepath="E:\\news.txt";
//在内存里开辟空间
File file = new File(filepath);
try {
//将内存传递到文件中
file.createNewFile();
System.out.println("ok");
} catch (IOException e) {
e.printStackTrace();
}
}
![image-20220314161658885]()
//输出流
@Test
public void writefile() throws IOException {
//得到路径
String filepath="e:\\a.txt";
FileOutputStream fileOutputStream=null;
创建对象
fileOutputStream = new FileOutputStream(filepath);
String str="hello world";
//将字符串str给字节数组,并创建文件
fileOutputStream.write(str.getBytes());
}
public void readFile1() throws IOException {
//得到路径
String filepath=("e:\\hello.txt");
int readdate=0;
int readlen=0;
byte[] buff=new byte[8];
//创建对象
File file = new File(filepath);
//将对象创建到文件中
file.createNewFile();
//创建对象来读取文件
FileInputStream fileInputStream=new FileInputStream(filepath);
while((readlen=fileInputStream.read(buff))!=-1){
//read读取文件
System.out.print(new String(buff,0,readlen));
}
fileInputStream.close();
}
//文件拷贝
public static void main(String[] args) throws IOException {
String filepath1="E:\\\\2.jpg";
String filepath2="E:\\\\3.jpg";
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
//创建对象用来输入
fileInputStream=new FileInputStream(filepath1);
//创建对象用来输出
fileOutputStream=new FileOutputStream(filepath2);
byte[] buf=new byte[1024];
int readln=0;
//读取文件
while ((readln=fileInputStream.read(buf))!=-1){
//write创建文件,并输出
fileOutputStream.write(buf,0,readln);
}
System.out.println("ok");
fileInputStream.close();
fileOutputStream.close();
}
![image-20220315213131199]()
package Text;
import java.io.*;
public class Demo1 {
public static void main(String[] args) throws IOException {
String filepath = "e:\\mytemp";
//创建对象
File file = new File(filepath);
当文件不存在创建目录
if (file.exists()) {
System.out.println("目录创建失败");
} else {
//创建目录
file.mkdir();
System.out.println("目录创建成功");
}
String sonpath = "e:\\mytemp\\hello.txt";
//创建对象,当文件不存在创建文件
File file1 = new File(sonpath);
if (!file1.exists()) {
//创建文件
file1.createNewFile();
//通过buff创建filewrite对象再sonpath路径下
BufferedWriter bw = new BufferedWriter(new FileWriter(sonpath));
//输出字符
bw.write("hello world");
//关闭
bw.close();
} else {
System.out.println("文件创建成功");
}
}
}
![image-20220315214556974]()
package Text;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo2 {
public static void main(String[] args) throws IOException {
String filepath="e:/c.txt";
String line="";
int count=0;
//buffread通过fileread方法在filepath路径里创建对象
BufferedReader br = new BufferedReader(new FileReader(filepath));
// 通过循环读取字符,readline为换行
while ((line=br.readLine())!=null){
System.out.println(++count+line);
}
br.close();
}
}
![image-20220315215703455]()
package Text;
import java.io.*;
public class Demo2 {
public static void main(String[] args) throws IOException {
String filepath="e:/c.txt";
String line="";
int count=0;
//将字节流变成转换流最后再变成处理流。处理流最好,因为处理流有许多节点流的方式,最后将文件以gbk编码方式输出
BufferedReader gbk = new BufferedReader(new InputStreamReader
(new FileInputStream(filepath), "gbk"));
// BufferedReader br = new BufferedReader(new FileReader(filepath));
while ((line=gbk.readLine())!=null){
System.out.println(++count+line);
}
gbk.close();
}
}
![image-20220315220847591]()
package Text;
import java.io.*;
import java.util.Properties;
public class Demo4 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load(new FileReader("src\\dog.properties"));
//获得name属性
String name = properties.get("name")+"";//object装换成string
//获得age属性
int age = Integer.parseInt(properties.get("age")+"");//object装换成int
//获得color属性
String color = properties.get("color")+"";//object装换成string
Dog dog = new Dog(name,age,color);
// properties.list(System.out);
System.out.println(dog);
String filepath="e:\\dog2.dat";
//object创建对象通过fileout创建在filepath地址里
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filepath));
//将dog的属性输出通过writeobject
oos.writeObject(dog);
oos.close();
}
}
class Dog implements Serializable{
private String name;
private int age;
private String color;
public Dog(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}