IO与文件「建议收藏」

IO与文件「建议收藏」IO 与文件 File File 类的一个对象 代表一个文件或一个文件目录 俗称文件夹 package com atguigu java import java io File import java io IOException public class FileTest public static void main String args

IO与文件

File

File类的一个对象,代表一个文件或一个文件目录(俗称文件夹)

package com.atguigu.java;

import java.io.File;
import java.io.IOException;

public class FileTest {

public static void main(String[] args) throws IOException {

File file1 = new File("hello.txt");//相对路径,相对当前module
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getPath());
System.out.println(file1.getName());
System.out.println(file1.getParent());
System.out.println(file1.length());
System.out.println(file1.lastModified());


String[] list = file1.list();
for(String s: list){

System.out.println(list);
}
File[] files = file1.listFiles();
for(File f:files){

System.out.println(files);
}
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
System.out.println(file1.exists());
System.out.println(file1.canRead());
System.out.println(file1.isHidden());

if(!file1.exists())
file1.createNewFile();
else {

file1.delete();
}
file1.mkdir();
file1.mkdirs();
}
}

File类中涉及到关于文件或文件目录的创建,删除,重命名,修改时间,文件大小等方法,并未涉及到写入或读取文件内容的操作。如果需要读取或写入文件内容,必须使用IO流来完成。

流的分类

字节流,字符流

输入流,输出流

节点流,处理流

流的体系

缓冲流

关闭流的时候先关闭外面的,再关闭里面的。(关闭外层流的时候,内层流也会自动的关闭)
缓冲流读写速度更快。读写会在内存中开辟一块儿空间。

转换流

提供了字节流和字符流之间的转换

InputStreamReader

OutputStreamWriter
实例:utf-8文件转化为gbk文件

package com.atguigu.java;

import java.io.*;

public class HelloWorld {

public static void main(String[] args) throws IOException {

File file1 = new File("dbcp.txt");
File file2 = new File("dbcp_bgk.txt");

FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);

InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");

char[] cbuf = new char[20];

int len;
while ((len = isr.read(cbuf)) != -1){

osw.write(cbuf,0,len);
}
isr.close();
osw.close();


}
}

标准输入输出流

标准的输入输出流
System,in 标准的输入流,默认从键盘输入,类型是InputStream
System,out 标准的输出流,默认从控制台输出,类型是 PrintStream,其是OutputStream的子类

System类的SetIn()/Setout()方式重新指定输入和输出流

练习:从键盘输入字符串,要求读取到的整行字符串转换成大写输出、然后继续进行输入操作

package com.atguigu.java;

import java.io.*;

public class HelloWorld {

public static void main(String[] args) throws IOException {


InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);
String data;
while(true){

data = br.readLine();
if("e".equals(data) || "exit".equals(data))
break;
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
br.close();


}
}

打印流

PrintStream和PrinterWriter提供了一系列重载方法print()和println()
将System.out.println()方法打印到指定文件

package com.atguigu.java;

import java.io.*;

public class HelloWorld {

public static void main(String[] args) throws IOException {

PrintStream ps = null;
try {

FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
ps = new PrintStream(fos,true);
if(ps != null){

System.setOut(ps);
}

for(int i = 0;i <= 255;i ++){

System.out.println((char)i);
if(i % 50 == 0)
System.out.println();
}

}catch (Exception e){

ps.close();
}

}
}

数据流

DataInputStream和DataOutputStream

package com.atguigu.java;

import java.io.*;

public class HelloWorld {

public static void main(String[] args) throws IOException {

DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("刘建辰");
dos.flush();
dos.writeInt(23);
dos.writeBoolean(true);
dos.flush();
dos.close();
}
}

对象流

ObjectInputStream和ObjectInputStream
对象要想可序列化,对象类必须实现Serializable接口
作用:可以把java中的对象写入到数据源中, 也能把对象从数据源中还原回来
该对象必须指定静态类型常量serialVersionUID
static和transient修饰的属性不能被序列化

package com.atguigu.java;

import java.io.*;

public class HelloWorld {

public static void main(String[] args){

ObjectOutputStream oos = null;
try {

oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("我爱北京天安门"));
oos.flush();
}catch (IOException e){

e.printStackTrace();
}finally {

if(oos != null)
{

try {

oos.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}


ObjectInputStream ois = null;
try {

ois = new ObjectOutputStream(new FileInputStream("object.dat"));
Object object = ois.readObject();
String str = (String)object;
} catch (IOException e) {

e.printStackTrace();
} catch (ClassNotFoundException e) {

e.printStackTrace();
} finally {

if(ois != null) {

try {

ois.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}
}
}

RandomAccessFile类

RandomAccessFile 直接继承于Object类,实现了 DataInput和DataOutput接口

RandomAccessFile 既可以作为一个输入流,又可以作为一个输出流

“r”以只读方式打开 “rw”打开以便读取和写入 “rwd”打开以便读取和写入:同步文件内容的更新 “rws”打开以便读取和写入:同步文件内容和元数据的更新

如果RandomAcessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建,如果写出到的文件存在,则会对原有文件内容进行覆盖。(默认情况下,从头覆盖)。

编程小号
上一篇 2025-02-27 13:57
下一篇 2025-03-03 10:57

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/125621.html