知识点:文件读写 (字符流和字符流),属性文件的读取与文件的切割合并

来源:互联网 发布:郑州大学网络教育学费 编辑:程序博客网 时间:2024/06/06 18:58

import java.io.*;//我自己的代码都是写具体的导入哪些类,但在这里有点过长,于是我换了*

/**
 * 文件读写 (字符流和字符流),通过键盘写入,输出到终端,读出文件,写到硬盘(文件),对一个非文本文件进行复制
 * @author Administrator
 */
public class Text12 {
 
 public static void keyBoard() {
  //通过键盘写入,输出到终端(由键盘输入可不释放资源,但必刷)
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
  
  String str = null;
  try {
   while((str = br.readLine()) != null) {
    try {
     bw.write(str.toUpperCase());
     bw.newLine();
     bw.flush(); //必刷
    } catch (IOException e) {
     new RuntimeException("写入出错");
    }
   }
  } catch (IOException e) {
   new RuntimeException("读出错");
  }
 }
 //读出文件,写到硬盘(文件)
 public static void file() {
  BufferedReader br = null;
  BufferedWriter bw = null;
  try {
   br = new BufferedReader(new FileReader("Text11.java"));
  } catch (FileNotFoundException e1) {
   new RuntimeException("找不到文件");
  }
  try {
   bw = new BufferedWriter(new FileWriter("Text11.txt"));
  } catch (IOException e1) {
   new RuntimeException("新建出错");
  }
  
  String str = null;
  try {
   while((str = br.readLine()) != null) {
    try {
     bw.write(str.toUpperCase());
     bw.newLine();
     bw.flush(); //必刷
    } catch (IOException e) {
     new RuntimeException("写入出错");
    }
   }
  } catch (IOException e) {
   new RuntimeException("读出错");
  }
  
 }
 //对一个非文本文件进行复制
 public static void music() {
  BufferedInputStream is = null;
  BufferedOutputStream os = null;
  int ch;
  
  try {
   is = new BufferedInputStream(new FileInputStream("Chen - The Best Luck.mp3"));
  } catch (FileNotFoundException e) {
   new RuntimeException("找不到文件");
  }
  try {
   os = new BufferedOutputStream(new FileOutputStream("Chen - The Best Luck3.mp3"));
  } catch (FileNotFoundException e) {
   new RuntimeException("新建音乐失败");
  }
  
  try {
   while((ch = is.read()) != -1) {
    os.write(ch);
    //os.flush();不一定要刷新
   }
  } catch (IOException e) {
   new RuntimeException("写入音乐失败");
  }
 }
 public static void main(String args[]) {
  //keyBoard();
  //file();
  music();
 }
}

思考与感悟:

字符流跟字节流都是比较死的东西,熟悉了就好了。当然不要忘了关流(关闭与流相关的系统资源,你的流对象,垃圾回收器会帮你回收的,你放心),还有刷新,因为数据都是通过先写到缓存的流对象里面再写到硬盘的,你要是怕麻烦就用PrintWriter类好了,它自动给你刷新。还有换行。

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

/**
 * 文件递归查询,属性类(软件设定5次就不能用了),切割合并文件
 * @author Administrator
 *
 */
public class Text13 {
 //文件递归查询
 public static void showFile(File dir) {
  if (dir.isDirectory()) {
   System.out.println( dir.getName());
  }
  File[] file = dir.listFiles();
  for (File f:file) {
   if (f.isDirectory()) {
    showFile(f);
   }
   else
   System.out.println("|--" + f.getName());
  }
 }
 
 //属性类(软件设定5次就不能用了)
 public static void property() {
  File file = new File("F:\\黑马\\Java基础知识\\count.property");
  FileInputStream is = null;
  FileOutputStream os = null;
  Properties pro = new Properties();
  int count = 0;
  
  try {
   is = new FileInputStream(file);
  } catch (FileNotFoundException e1) {
   new RuntimeException("新建的输入流失败");
  }
  if (!file.exists()) {
   try {
    file.createNewFile();
   } catch (IOException e) {
    new RuntimeException("新建文件错误");
   }
  }
  try {
   pro.load(is);
  } catch (IOException e) {
   new RuntimeException("属性列表加载错误");
  }
  String value  = pro.getProperty("time");
  if (value != null) {
  count = Integer.parseInt(value);
  }
  count = count + 1;
  if (count >= 5) {
   System.out.println("不能再用了");
  }
  pro.setProperty("time", count + "");
  //输出流的位置很重要,it.hasNext();在取count前新建的话,会在count自增之前把旧的的文件覆盖,这样count永远为1
  try {
   os = new FileOutputStream(file);
  } catch (FileNotFoundException e1) {
   new RuntimeException("新建的输出流失败");
  }
  try {
   pro.store(os, "time");
  } catch (IOException e) {
   new RuntimeException("存储错误");
  }
 }
 //切割文件
 public static void splitFile() {
  //jepg即jpg
  File file = new File("E:\\ldx.jpg");
  FileInputStream is = null;
  FileOutputStream os = null;
  byte[] buf = new byte[1024*20];
  int len = 0;
  int count = 0;
  
  try {
   is = new FileInputStream(file);
  } catch (FileNotFoundException e) {
   new RuntimeException("新建的输入流失败");
  }
  
  try {
   while((len = is.read(buf))!= -1) {
    count++;
    os = new FileOutputStream("E:\\" + count + ".part");
    os.write(buf, 0, len);
   }
  } catch (IOException e) {
   new RuntimeException("读数据失败");
  }
 }
 //合并文件
 public static void combineFile() {
  byte[] buf = new byte[1024*20];
  FileOutputStream os = null;
  int len = 0;
  ArrayList<FileInputStream> is = new ArrayList<FileInputStream>();
  try {
  is.add(new FileInputStream("E:\\1.part"));
  is.add(new FileInputStream("E:\\2.part"));
  is.add(new FileInputStream("E:\\3.part"));
  is.add(new FileInputStream("E:\\4.part"));
  is.add(new FileInputStream("E:\\5.part"));
  } catch (FileNotFoundException e) {
   new RuntimeException("新建的输入流失败");
  }
  //为什么老师这里不是final,而我这里要。
  final Iterator<FileInputStream> it = is.iterator();
  Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {

   @Override
   public boolean hasMoreElements() {
    return it.hasNext();
   }

   @Override
   public FileInputStream nextElement() {
    return it.next();
   }
   
  };
  SequenceInputStream sis = new SequenceInputStream(en);
  
  try {
   os = new FileOutputStream("E:\\1.jpg");
  } catch (FileNotFoundException e) {
   new RuntimeException("新建的输出流失败");
  }
  try {
   while((len = sis.read(buf))!=-1) {
    os.write(buf, 0, len);
   }
  } catch (IOException e) {
   new RuntimeException("读数据失败");
  }
  
  try {
   if (sis != null)
   sis.close();
  } catch (IOException e) {
   new RuntimeException("合并流关闭失败");
  }
  try {
   if (os != null)
   os.close();
  } catch (IOException e) {
   new RuntimeException("输出流关闭失败");
  }
 }
 public static void main(String args[]) {
  /*File dir = new File("F:\\黑马");
  showFile(dir);*/
  //property();
  //splitFile();
  combineFile();
 }

}

思考与感悟:

Properties就是一些写入取出。文件类型这个有必要说一下。我们要习惯把路径设成文件类型,这样可以给我们提供很多便利。因为File有很多实用方法。

文件切割就没啥的,也没用到什么关键的类,就是读的时候分块放到不同的小块文件里而已。我们的read方法本身就可以把数据暂存到一个Byte[]里面。

合并的话用到了SequenceInputStream类,这个类又用到了Enumeration类。这些都是这样用的。你明白为什么但是你还是没想到有什么更简单的方式。


 

0 0