[Java手记]IO操作-输入输出操作
IO操作-输入输出操作
IO操作的一些释义
字节输入流:FileInputStream
字节输出流:FileOutputStream
字符输入流:FileReader
字符输出流:FileWriter
缓冲输字符入流:BufferedReader
缓冲输字符出流:BufferedWriter
打印流:PrintWriter
对象输入流:ObjectInputStream
对象输出流:ObjectOutputSteam
文件相关简单操作
public static void main(String[] args) throws IOException {
File file=new File("D:\\Download\\aaa\\s.jpg");
//创建新文件
file.createNewFile();
//创建新文件夹
file.mkdir();
//判断路径是否存在
System.out.println(file.exists());
//判断是不是文件
System.out.println(file.isFile());
//判断是不是目录
System.out.println(file.isDirectory());
//目录下文件列表
System.out.println(Arrays.toString(file.list()));
//获取绝对路径
System.out.println(file.getAbsoluteFile());
//删除文件
file.delete();
//重名名
file.renameTo(new File("D:\\Download\\aaa\\b.jpg"));
}
字节输入输出流
1、字节输入流
public static void main(String[] args) {
File file=new File("D:\\Download\\aaa\\2.txt");
FileInputStream fis = null;
try {
//字节输入流
fis=new FileInputStream(file);
//获取文件长度
System.out.println(fis.available());
//读取文件并返回读取到的位置,如果读取完毕返回-1
System.out.println(fis.read());
//长度为1024的数组,存储读取到的文件
byte[] b=new byte[1024];
//读取一部分内容放进数组
int len= fis.read(b);
while (len!=-1){
String data=new String(b);//将byte数组转换成字符串
System.out.println(data);
len=fis.read(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、字节输出流
public static void main(String[] args) {
//字节输出流
String s="你好!";
FileOutputStream fos=null;
try {
fos=new FileOutputStream("D:\\Download\\aaa\\3.txt" ,true);//.true是追加写入,默认是覆盖写入
//将要写入的字符串转换成字节数组
byte[] words=s.getBytes();
//写入文件
fos.write(words,0,words.length);
//清空缓存区数据,并强制写入
fos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输入输出流
1、字符输入流
public static void main(String[] args) {
//字符输入流
File file=new File("D:\\Download\\aaa\\2.txt");
FileReader fr=null;
try {
fr=new FileReader(file);
char[] c=new char[1024];
int len=fr.read(c);
while (len!=-1){
//char类型强制转换成字符串
String s=new String(c);
System.out.println(s);
len=fr.read();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、字符输出流
public static void main(String[] args) {
//字节输出流
String s="你好!";
FileWriter fos=null;
try {
fos=new FileWriter("D:\\Download\\aaa\\3.txt" ,true);//.true是追加写入,默认是覆盖写入
//写入文件
fos.write(s);
//清空缓存区数据,并强制写入
fos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲字符输入输出流
1、缓冲字符输入流
public static void main(String[] args) {
//缓冲输入流
File file=new File("D:\\Download\\aaa\\2.txt");
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader(file));
String str=br.readLine();//逐行读取
while (str!=null){
System.out.println(str);
str=br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、缓冲字符输出流
public static void main(String[] args) {
//缓冲输入流
String s="你好123!";
BufferedWriter br=null;
try {
br=new BufferedWriter(new FileWriter("D:\\Download\\aaa\\3.txt"));
br.write(s+"\r\n");//+"\r\n"是换行输出
br.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对象输出输入流
1、对象输出流
//Stu类
public class Stu implements Serializable { //序列化接口
public Stu(String name, int age) {
this.name = name;
this.age = age;
}
private String name;
private int age;
//Main类
public static void main(String[] args) {
//对象输出流
Stu s1=new Stu("张三",12);
Stu s2=new Stu("李四",11);
Stu s3=new Stu("王五",13);
ArrayList<Stu> a1=new ArrayList<Stu>();
a1.add(s1);
a1.add(s2);
a1.add(s3);
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream("D:\\Download\\aaa\\4.txt"));
oos.writeObject(a1);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、对象输入流
public static void main(String[] args) {
//对象输入流
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream("D:\\Download\\aaa\\4.txt"));
ArrayList<Stu> a1=(ArrayList<Stu>) ois.readObject();
for (Stu stu:a1){
System.out.println(stu.name);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
PrintWriter打印流
数据显示和打印
只能输出
打印到控制台
public static void main(String[] args) {
//打印流输出
String s=null;
s="你好!";
PrintWriter pw=new PrintWriter(System.out);
pw.println(s);
pw.close();
}
输出到文件
public static void main(String[] args) {
//打印流输出
String s=null;
s="你好!";
PrintWriter pw=null;
try {
pw=new PrintWriter(new FileWriter("D:\\Download\\aaa\\3.txt",true));
pw.write(s +"\r\n");
} catch (IOException e) {
e.printStackTrace();
}finally {
pw.close();
}
}
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
屠龙少年!
喜欢就支持一下吧