Java第十章IO
十、IO
File类
java.io.File类 1.凡是与输入、输出相关的类、接口等都定义在java.io包下 2.File是一个类,可以有构造器创建其对象。此对象对应着一个文件或文件目录 3.File类对象是与平台无关的 4.File中的方法,仅涉及到如何创建、删除、重命名等等,只要涉及文件内容的,File无能为力。必须有IO流来完成 5.File类的对象常作为IO流的具体类的构造器的形参。路径: 绝对路径:包括盘符在内的完整的文件路径 相对路径:在当前文件目录下的文件的路径
File方法
getName()//获取文件名 getPath()//获取文件路径 getAbsoluteFile()//获取绝对文件名 getParent()//获取上一层的文件路径//相对路径时null getAbsolutePath()//获取绝对路径 renameTo(File newName)//重命名 file1.renameTo(file2)//file1重命名为file2.要求file文件一定存在,file2一定不存在exists()//文件存在 canWrite()//文件可写 canRead()//文件可读 isFile()//是文件吗 isDirectory()//是文件目录 lastModfied()//最后修改时间 length()//内容长度sysout(new Date(file1.lastModified()));createNewFile() delete() mkDir():创建一个文件目录,只有在上层文件目录存在的情况下,才能创建 mkDirs():创建一个文件目录,若上层文件目录不存在,一并创建。 list() :得到文件目录的内容,用字符串数组接收 listFiles():得到文件目录的内容,用文件数组接收。
IO的体系
流
FileInputStream
//read() @Testpublic void test1(){FileInputStream fis = null;try {File file1 = new File("g:/IO/hello2.txt");fis = new FileInputStream(file1);int b = fis.read();while(b!=-1){System.out.print((char)b);b = fis.read();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fis != null){try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
//read(byte[] byte)
@Test
public void test2(){
FileInputStream fis = null;
try {
File file2 = new File("g:/IO/hello2.txt");
fis = new FileInputStream(file2);
byte[] b = new byte[5];
int len ;
while((len = fis.read(b))!= -1){
for(int i= 0 ;i<len;i++){
}
String str = new String(b,0,len);
System.out.print(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileOutputStream
输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建,若存在,会将原有的文件覆盖
@Testpublic void test3(){File file3 = new File("g:/IO/hello3.txt");FileOutputStream fos = null;try{fos = new FileOutputStream(file3);fos.write(new String("I love myself!").getBytes());}catch(Exception e){e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
FileReaderFileWriter
@Test public void test5(){FileReader fr = null;FileWriter fw = null;try {File file1 = new File("C:\\Users\\dell\\Desktop\\新建文件夹\\java基础.txt");File file2 = new File("C:\\Users\\dell\\Desktop\\新建文件夹\\111.txt");fr = new FileReader(file1);fw = new FileWriter(file2);int len;char[] c = new char[1000];while((len=fr.read(c))!=-1){fw.write(c, 0, len);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fr!=null){try {fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fw!=null){try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
BufferedInputStream/BufferedOutputStream
@Testpublic void test6(){FileInputStream fis = null;FileOutputStream fos = null;BufferedInputStream bis = null;BufferedOutputStream bos = null;try {File file1 = new File("D:\\百度网盘下载\\java\\第10章:IO(day15-day16)\\day15\\day15_04IO流概述.wmv");File file2 = new File("D:\\百度网盘下载\\java\\第10章:IO(day15-day16)\\day15\\1.wmv");fis = new FileInputStream(file1);fos = new FileOutputStream(file2);bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);int len;byte[] b = new byte[1024];while((len = bis.read(b))!= -1){bos.write(b, 0, len);bos.flush();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bis!= null){try {bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(bos!=null){try {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
BufferedWriter/BufferedReader
@Testpublic void test7(){BufferedWriter bw = null;BufferedReader br = null;try {File file1 = new File("");File file2 = new File("");FileReader fr = new FileReader(file1);FileWriter fw = new FileWriter(file2);br = new BufferedReader(fr);bw = new BufferedWriter(fw);String str;while((str = br.readLine())!= null){bw.write(str);bw.newLine();//换行bw.flush();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bw!=null){try {bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(br!=null){try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
转换流:
InputStreamReader / OutputStreamWriter(前提:数据是文本文件)
/*如何实现字节流与字符流之间的转换:* 转换流:InputStreamReader OutputStreamWriter* 编码:字符串 ---->字符数组* 解码:字符数组--->字符串*/@Testpublic void test8(){BufferedReader br = null;BufferedWriter bw = null;try {File file1 = new File("");File file2 = new File("");FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);InputStreamReader isr = new InputStreamReader(fis, "UTF-8");OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");br = new BufferedReader(isr);bw = new BufferedWriter(osw);String str;while((str = br.readLine())!=null){bw.write(str);bw.newLine();bw.flush();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bw!=null){try {bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(br!=null){try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
标准的输入输出流:
标准的输出流:System.out
标准的输入流:System.in
str.toUpperCase();小写变大写
注:使用FileReader、FileWriter 可以实现文本文件的复制
对于非文本文件(视频文件、音频文件、图片),只能使用字节流
打印流:
打印流:字节流:PrintStream 字符流:PrintWriter //创建打印输出流,设置为自动刷新模式(写入换行符或字节'\n'时都会刷新输出缓存区)if(ps != null){//把标准输出流(控制台输出)改成文件System.setOut(ps);}
数据流:
数据流:用来处理基本数据类型、String、字节数组的数据:DataInputStream DataOutputStream
对象流:
要实现序列化的类:1.要求此类是可序列化的,实现Serializable接口2.要求类的属性同样的要实现Serializable接口3.提供一个版本号,private static fianl long serialVersionUID4.使用static 或 transient 修饰的属性,不可实现序列化对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储到硬盘文件中对象的反序列化过程,将二进制流通过ObjectInputStream转换为内存中的对象。
RandomAccessFile:
/** RandomAccessFile:支持随机访问* 1.即可以充当一个输入流,又可以充当一个输出流* 2.支持从文件的开头读取、写入* 3.支持从任意位置的读入、写入(插入)*/@Testpublic void testRandomAccess(){RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try {raf1 = new RandomAccessFile(new File("hello.txt"),"r");raf2 = new RandomAccessFile(new File("hello1.txt"),"rw");byte[] b = new byte[5];int len;while((len = raf1.read(b)) != -1){raf2.write(b,0, len);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(raf1 != null){try {raf1.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(raf2!=null){try {raf2.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
/*
* 实现文件指定位置插入数据
*/
@Test
public void test2(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello1.txt"), "rw");
raf.seek(4);//把指针位置移到第4个位置
byte[] b = new byte[10];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf.read(b))!=-1){
sb.append(new String(b,0,len));//把文件中当前指针位置后面的数据存到StringBuffer
}
raf.seek(4);
raf.write("YHS".getBytes());
raf.write(sb.toString().getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(raf!=null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
转载于:https://www.cnblogs.com/yangHS/p/10699729.html
总结
- 上一篇: 程序退出
- 下一篇: Java 取得文件名的后缀