知识大全 java 常用IO操作

Posted 文件

篇首语:努力尽今夕,少年犹可夸。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 java 常用IO操作相关的知识,希望对你有一定的参考价值。

java 常用IO操作  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  package syj util;

  import java io BufferedReader;        import java io BufferedWriter;        import java io ByteArrayInputStream;        import java io ByteArrayOutputStream;        import java io File;        import java io FileInputStream;        import java io FileOutputStream;        import java io FileReader;        import java io FileWriter;        import java io IOException;        import java io InputStream;        import java io InputStreamReader;        import java io ObjectInputStream;        import java io ObjectOutputStream;        import java io OutputStream;        import java io PrintWriter;        import java io StringReader;        import java util Arrays;

  /**         * <p>         * Title:IO工具类         * </p>         *         * <p>         * Description:常用的IO操作封装         * </p>         *         * <p>         * Copyright: 转载请注明出处<A >    </A>     * </p>         *         * @author 孙钰佳         * @main <A >mailto: >    </A>     * @date Jun : : PM         */

  public class IOUtil          /**          * 缓冲区大小 MB          */         private static final int BUFFER_SIZE = * ;

  /**          *          * Description: 将输入流输出到输出流          *         * @param in          *            输入流          * @param out          *            输出流          * @param bufferSize          *            缓冲区大小          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static void in OutStream(InputStream in OutputStream out

  int bufferSize) throws IOException           byte[] buffer = new byte[bufferSize];// 缓冲区          for (int bytesRead = ; (bytesRead = in read(buffer)) != ;)            out write(buffer bytesRead);           Arrays fill(buffer (byte) );                  

  package syj util;

  import java io BufferedReader;        import java io BufferedWriter;        import java io ByteArrayInputStream;        import java io ByteArrayOutputStream;        import java io File;        import java io FileInputStream;        import java io FileOutputStream;        import java io FileReader;        import java io FileWriter;        import java io IOException;        import java io InputStream;        import java io InputStreamReader;        import java io ObjectInputStream;        import java io ObjectOutputStream;        import java io OutputStream;        import java io PrintWriter;        import java io StringReader;        import java util Arrays;

  /**         * <p>         * Title:IO工具类         * </p>         *         * <p>         * Description:常用的IO操作封装         * </p>         *         * <p>         * Copyright: 转载请注明出处         * </p>         *         * @author 孙钰佳         * @main          * @date Jun : : PM         */

  public class IOUtil          /**          * 缓冲区大小 MB          */         private static final int BUFFER_SIZE = * ;

  /**          *          * Description: 将输入流输出到输出流          *         * @param in          *            输入流          * @param out          *            输出流          * @param bufferSize          *            缓冲区大小          * @throws IOException          * @mail           * @since Jun : : PM          */         public static void in OutStream(InputStream in OutputStream out

  int bufferSize) throws IOException           byte[] buffer = new byte[bufferSize];// 缓冲区          for (int bytesRead = ; (bytesRead = in read(buffer)) != ;)            out write(buffer bytesRead);           Arrays fill(buffer (byte) );                       view plaincopy to clipboardprint?         /**          *          * Description: 读取文件返回字节数组流          *          * @param file          *            文件          * @return 字节数组流          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static ByteArrayOutputStream readFileToByteStream(File file)           throws IOException

  FileInputStream fis = null;          ByteArrayOutputStream bos = null;          try            fis = new FileInputStream(file);           bos = new ByteArrayOutputStream();           in OutStream(fis bos BUFFER_SIZE);          finally            if (fis != null)            fis close();         

  return bos;

  

  /**          *          * Description:读取文件返回字节数组          *          * @param file          *            文件          * @return 字节数组          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static byte[] readFileToByteArray(File file) throws IOException

  ByteArrayOutputStream bos = null;          try            bos = readFileToByteStream(file);          finally            if (bos != null)            bos close();         

  return bos toByteArray();

  

  /**          *          * Description:读取文件内容          *          * @param file          *            文件          * @return String内容          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static String readFileToString(File file) throws IOException

  StringBuffer sb = null;          BufferedReader in = null;          try            in = new BufferedReader(new FileReader(file));           sb = new StringBuffer();           for (String line; (line = in readLine()) != null;)             sb append(line + \\r\\n );                     finally            if (in != null)            in close();         

  return sb toString();

  

  /**          *          * Description:复制文件          *          * @param src          *            源文件          * @param dest          *            目标文件          * @param cover          *            是否覆蓋          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static void copyFile(File src File dest boolean cover)           throws IOException

  /**          *          * Description: 读取文件返回字节数组流          *          * @param file          *            文件          * @return 字节数组流          * @mail           * @since Jun : : PM          */         public static ByteArrayOutputStream readFileToByteStream(File file)           throws IOException

  FileInputStream fis = null;          ByteArrayOutputStream bos = null;          try            fis = new FileInputStream(file);           bos = new ByteArrayOutputStream();           in OutStream(fis bos BUFFER_SIZE);          finally            if (fis != null)            fis close();         

  return bos;

  

  /**          *          * Description:读取文件返回字节数组          *          * @param file          *            文件          * @return 字节数组          * @throws IOException          * @mail           * @since Jun : : PM          */         public static byte[] readFileToByteArray(File file) throws IOException

  ByteArrayOutputStream bos = null;          try            bos = readFileToByteStream(file);          finally            if (bos != null)            bos close();         

  return bos toByteArray();

  

  /**          *          * Description:读取文件内容          *          * @param file          *            文件          * @return String内容          * @throws IOException          * @mail           * @since Jun : : PM          */         public static String readFileToString(File file) throws IOException

  StringBuffer sb = null;          BufferedReader in = null;          try            in = new BufferedReader(new FileReader(file));           sb = new StringBuffer();           for (String line; (line = in readLine()) != null;)             sb append(line + \\r\\n );                     finally            if (in != null)            in close();         

  return sb toString();

  

  /**          *          * Description:复制文件          *          * @param src          *            源文件          * @param dest          *            目标文件          * @param cover          *            是否覆蓋          * @throws IOException          * @mail           * @since Jun : : PM          */         public static void copyFile(File src File dest boolean cover)           throws IOException

  view plaincopy to clipboardprint?          FileInputStream in = null;          FileOutputStream out = null;          try            if (!dest exists())             dest createNewFile();           else if (dest exists() && cover)             dest delete();            dest createNewFile();           else

  return;

  

  in = new FileInputStream(src);           out = new FileOutputStream(dest);           in OutStream(in out BUFFER_SIZE);

   finally            try             if (in != null)             in close();           finally             if (out != null)             out close();                             

  /**          *          * Description:写文件          *          * @param file          *            文件          * @param str          *            内容          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static void writeFile(File file String str) throws IOException

  PrintWriter out = null;          BufferedReader in = null;          try            if (!file exists())            file createNewFile();           in = new BufferedReader(new StringReader(str));           out = new PrintWriter(new BufferedWriter(new FileWriter(file)));           for (String line; (line = in readLine()) != null;)             out println(line);                     finally

  try             if (in != null)             in close();           finally             if (out != null)             out close();                             

  /**          *          * Description:从控制台读取一串字符串          *          * @return 读取的字符串          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static String readStringFromSystemIn() throws IOException         BufferedReader br = new BufferedReader(new InputStreamReader(System in));          try            return br readLine();          finally            if (br != null)            br close();                  

  /**          *          * Description:当ObjectInputStream对象调用          * readObject();时 会从ByteArrayInputStream流中反序列化出的对象          *          *          * @param bi          * @return          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static ObjectInputStream buildObjectInputStream(           ByteArrayInputStream bi) throws IOException

  return new ObjectInputStream(bi);

  

  /**          *          * Description:当ObjectOutputStream对象调用          * writeObject(o);时 o对象会序列化到ByteArrayOutputStream流中去          *          * @param bos          *            字节数组流          * @return 对象输出流          * @throws IOException          * @mail <A >mailto: >    </A>      * @since Jun : : PM          */         public static ObjectOutputStream buildObjectOutputStream(           ByteArrayOutputStream bos) throws IOException

  return new ObjectOutputStream(bos);

  

  public static BufferedReader buildBufferedReader(String str)

  return new BufferedReader(new StringReader(str));

  

  public static ByteArrayInputStream buildByteArrayInputStream(String str)

  return new ByteArrayInputStream(str getBytes());

  

  public static ByteArrayInputStream buildByteArrayInputStream(byte[] bt)

  return new ByteArrayInputStream(bt);

  

  public static void main(String[] args) throws Exception           byte[] bootFileBytes = IOUtil readFileToByteArray(new File(            C:\\\\boot ini ));          System out println(new String(bootFileBytes));          String bootFileStr = readFileToString(new File( C:\\\\boot ini ));          System out println(bootFileStr);          System out println(new String(bootFileBytes) equals(bootFileStr));

  pyFile(new File( C:\\\\boot ini ) new File( C:\\\\boot ini )             true);

  IOUtil writeFile(new File( C:\\\\boot ini ) bootFileStr);          ByteArrayOutputStream bos = new ByteArrayOutputStream();          ObjectOutputStream oos = IOUtil buildObjectOutputStream(bos);          oos writeObject(new String( abcd ));

  ObjectInputStream ois = IOUtil buildObjectInputStream(IOUtil            buildByteArrayInputStream(bos toByteArray()));

cha138/Article/program/Java/hx/201311/25681

相关参考

知识大全 Java对各种文件的操作详解

Java对各种文件的操作详解  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  java中提供了io

知识大全 彻底明白Java的IO系统

彻底明白Java的IO系统  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  彻底明白Java的IO

知识大全 IO的阻塞与非阻塞、同步与异步以及Java网络IO交互方式

IO的阻塞与非阻塞、同步与异步以及Java网络IO交互方式  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一

知识大全 Java的文件IO机制

Java的文件IO机制  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Java有一个庞大的I/O

知识大全 java序列化--java.io.Serializable接口解析

java序列化--java.io.Serializable接口解析  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一

知识大全 Java的IO总结

Java的IO总结  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  知识点一:四大等级结构  ja

知识大全 java io读取文本文件

  publicclassReadSql  publicstaticvoidreadsql(StringfilePath)throwsException  Stringencoding=gbk;  F

知识大全 Java NIO(异步IO)Socket通信例子

JavaNIO(异步IO)Socket通信例子  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  /

知识大全 Java IO之有缓冲的文本输入

JavaIO之有缓冲的文本输入  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  输入就是Input

知识大全 java常用的ant的操作方法

  我常用的ant的操作方便自己查询所以传到网上如果有朋友觉得不够请补充  主要的内容有  ()建立一个项目  ()建立属性  ()对数据库的操作  ()javac编译  ()删除目录  ()建立目录