File类:
package io;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
/*
File类的使用.
1. File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)
2. File类声明在java.io包下
3. File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,
并未涉及到写入或读取文件内容的操作。如果需要读取或写入文件内容,必须使用I0流来完成。
4.后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的”终点"
*/
/*
public String getAbsolutePath():获取绝对路径
public String getPath() :获取路径
public String getName() :获取名称
public String getParent(): 获取上层文件目录路径。若无,返@null
public long Length() :获取文件长度(即:字节数)。不能获取目录的长度。
public long lastModified() :获取最后一次的修改时间,毫秒值
public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File數組
public boolean renameTo(File dest);把文件重命名为指定的文件路径
public boolean isDirectory(): 判断是否是文件目录
public boolean isFile() :判断是否是文件
public boolean exists() :判断是否存在
public boolean canRead() :判断是否可读
public boolean canWrite() :判断是否可写
public boolean isHidden() :判断是否隐藏
public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建
public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
*/
public class FileTest {
public static void main(String[] args) {
FileTest fileTest=new FileTest();
fileTest.test1();
fileTest.test2();
//fileTest.test3();
fileTest.test4();
fileTest.test5();
fileTest.test6();
}
public void test1() {
File file=new File("hello.txt");
File file2=new File("F:\\javaa\\Test\\hello1.txt");
System.out.println(file);
System.out.println(file2);
File file3=new File("F:\\\\javaa","Test");
System.out.println(file3);
File file4=new File(file3,"hello4.txt");
System.out.println(file4);
System.out.println("");
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
System.out.println(file.getName());
System.out.println(file.getParent());
System.out.println(file.length());
System.out.println(new Date(file.lastModified()));
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getPath());
System.out.println(file2.getName());
System.out.println(file2.getParent());
System.out.println(file2.length());
System.out.println(file2.lastModified());
System.out.println("");
}
public void test2() {
File file=new File("F:\\Javaa\\Test");
String[] list=file.list();
System.out.println(Arrays.toString(list));
File[] files=file.listFiles();
System.out.println(Arrays.toString(files));
}
public void test3() {
//file应该存在,file2不应该存在
File file=new File("hello.txt");
File file2=new File("F:\\javaa\\Test\\hi.txt");
boolean b= file.renameTo(file2);
System.out.println(b);
}
public void test4() {
File file=new File("hi.txt");
System.out.println(file.isDirectory());
System.out.println(file.isFile());
}
public void test5() {
File file=new File("hello.txt");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
file.delete();
}
}
public void test6() {
File file=new File("F:\\Javaa\\Test\\io1");
file.mkdir();
}
}
流的分类:
字符流:
package io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//字符流不能处理图片
public class FileReaderWriterTest {
public static void main(String[] args) {
FileReaderWriterTest fileReaderWriterTest=new FileReaderWriterTest();
fileReaderWriterTest.test1();
fileReaderWriterTest.test2();
fileReaderWriterTest.test3();
}
public void test1() {
File file=new File("hi.txt");
try {
FileReader fReader=new FileReader(file);
//read():返回读入的一个字符,如果达到文件末尾则返回-1
int data;
while((data=fReader.read())!=-1) {
System.out.print((char)data);
}
System.out.println("");
fReader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void test2() {
File file=new File("hi.txt");
try {
FileReader fReader=new FileReader(file);
char[] ch=new char[10];
int len;
while((len=fReader.read(ch))!=-1) {
/*for(int i=0;i<len;i++) {
System.out.print(ch[i]);
}*/
String string=new String(ch,0,len);
System.out.println(string);
}
fReader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
}
}
public void test3() {
File file=new File("hello.txt");
try {
//true不覆盖原有文件
//false覆盖原有文件
FileWriter fileWriter=new FileWriter(file,true);
fileWriter.write("helloahh\n");
fileWriter.write("helloahh\n");
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字节流:
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//对于文本文件(.txt , .java,.c,.cpp),使用字符流处理
//对于非文本文件(.jpg , .mp3,.mp4, .avi,.doc, .ppt....),使用字节流处理
//字节流可以处理图片
public class FileInputOutputTest {
public static void main(String[] args) {
FileInputOutputTest fileInputOutputTest=new FileInputOutputTest();
fileInputOutputTest.test1();
fileInputOutputTest.test2();
}
public void test1() {
File file=new File("hi.txt");
FileInputStream fReader = null;
File file1=new File("hello.txt");
FileOutputStream fWriter=null;
try {
fReader = new FileInputStream(file);
fWriter=new FileOutputStream(file1);
byte[] ch=new byte[10];
int len;
while((len=fReader.read(ch))!=-1) {
for(int i=0;i<len;i++) {
fWriter.write(ch[i]);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void test2() {
File file=new File("aaa.jpg");
FileInputStream fi=null;
File file1=new File("bbb.jpg");
FileOutputStream fo=null;
try {
fi=new FileInputStream(file);
fo=new FileOutputStream(file1);
byte[] b=new byte[10];
int len;
while((len=fi.read(b))!=-1) {
fo.write(b,0,len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
转换流:
package io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class ChangeTest {
/*
转换流:属于字符流
InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputstreamWriter:将一个字符的输出流转换为字节的输出流
*/
public static void main(String[] args) {
ChangeTest changeTest=new ChangeTest();
changeTest.test1();
}
public void test1() {
FileInputStream fis=null;
InputStreamReader isr=null;
try {
fis=new FileInputStream("hi.txt");
//不指明则使用系统默认的字符集
isr=new InputStreamReader(fis,"UTF-8");
char[] ch=new char[20];
int len;
while((len=isr.read(ch))!=-1) {
String string=new String(ch,0,len);
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
缓冲流:
package io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//缓冲流可以提升流的读取写入速度
//原因:内部提供了一个缓冲区
public class BufferedTest {
public static void main(String[] args) {
BufferedTest bufferedTest=new BufferedTest();
bufferedTest.test1();
bufferedTest.test2();
}
public void test1() {
File file=new File("aaa.jpg");
File file2=new File("ccc.jpg");
FileInputStream fis=null;
FileOutputStream fos=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
fis=new FileInputStream(file);
fos=new FileOutputStream(file2);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
byte[] b=new byte[10];
int len;
while((len=bis.read(b))!=-1) {
bos.write(b,0,len);
//bos.flush();清空缓冲区
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//先关闭外层的流
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭外层流时,内层流会自动关闭
}
}
public void test2() {
File file=new File("hi.txt");
File file2=new File("hello.txt");
FileReader fis=null;
FileWriter fos=null;
BufferedReader bis=null;
BufferedWriter bos=null;
try {
fis=new FileReader(file);
fos=new FileWriter(file2);
bis=new BufferedReader(fis);
bos=new BufferedWriter(fos);
String data;
while((data=bis.readLine())!=null) {
//bos.write(data+'\n');
bos.write(data);
bos.newLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//先关闭外层的流
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭外层流时,内层流会自动关闭
}
}
}
其他:
package io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OtherStream {
public static void main(String[] args) {
OtherStream otherStream=new OtherStream();
otherStream.test1();
}
public void test1() {
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
while(true) {
try {
String data=br.readLine();
if("e".equalsIgnoreCase(data)) {
System.out.println("end");
break;
}
String s=data.toUpperCase();
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
//不能序列化static和transient修饰的成员
/*
自定义类需要满足如下的要求,方可序列化
1.需要实现接口: Serializable
2.当前类提供一一个全局常量: serialVersionUID
3.内部的所有属性也必须可序列化(默认基本数据类型都是可序列化的)
*/
public class ObjectTest {
public static void main(String[] args) {
ObjectTest objectTest=new ObjectTest();
objectTest.test1();
objectTest.test2();
}
//序列化过程:将内存中的java对象保存到磁盘中或通过网絡传输出去
//使用ObjectOutputStream实现
public void test1() {
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("aaa"));
oos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//反序列化
public void test2() {
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream("object.dat"));
String string=(String)ois.readObject();
System.out.println(string);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
RandomAccessFile:
package io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/*
RandomAccessFile的使用
1. RandomAccessFile直按继承于java.lang.object类,实现J DataInput和Dataoutput接口
2. RandomAccessFile既可以作为-个输入流,又可以作为一个输出流
3. 如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建
如果写出到的文件存在,则会对原有文件内容进行覆盖。
4.可以通过类似test3的操作完成插入的效果
*/
public class RandomAccessFileTest {
public static void main(String[] args) {
RandomAccessFileTest raft=new RandomAccessFileTest();
raft.test1();
raft.test2();
raft.test3();
}
public void test1() {
RandomAccessFile raf=null;
RandomAccessFile raf1=null;
try {
raf=new RandomAccessFile(new File("aaa.jpg"),"r");
raf1=new RandomAccessFile(new File("aaaaa.jpg"),"rw");
byte[] b=new byte[1024];
int len;
while((len=raf.read(b))!=-1) {
raf1.write(b,0,len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
raf1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void test2() {
RandomAccessFile raf=null;
try {
raf=new RandomAccessFile(new File("hello.txt"),"rw");
raf.seek(3);
raf.write("three".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void test3() {
RandomAccessFile raf=null;
try {
raf=new RandomAccessFile(new File("hi.txt"),"rw");
raf.seek(3);
byte[] b=new byte[20];
int len;
StringBuilder builder=new StringBuilder((int)new File("hi.txt").length());
while((len=raf.read(b))!=-1) {
builder.append(new String(b,0,len));
}
raf.seek(3);
raf.write("three".getBytes());
raf.write(builder.toString().getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}