大家好,又见面了,我是你们的朋友全栈君。
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流来完成。
关闭流的时候先关闭外面的,再关闭里面的。(关闭外层流的时候,内层流也会自动的关闭) 缓冲流读写速度更快。读写会在内存中开辟一块儿空间。
提供了字节流和字符流之间的转换
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();
}
}
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();
}
}
}
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168827.html原文链接:https://javaforall.cn