zoukankan      html  css  js  c++  java
  • JAVA进行基础的文件IO读写

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 public class TestIO {
     8     
     9     public void testRead(String path){
    10         FileInputStream is = null;
    11         File file = new File(path);
    12         try {
    13             is = new FileInputStream(file);
    14             byte[] byteBuffer = new byte[is.available()];
    15             int read = 0;
    16             while((read = is.read(byteBuffer))!= -1){
    17                 System.out.write(byteBuffer, 0, read);
    18             }
    19         } catch (FileNotFoundException e) {
    20             e.printStackTrace();
    21         } catch (IOException e) {
    22             e.printStackTrace();
    23         }finally{
    24             try {
    25                 if(is!=null){
    26                     is.close();
    27                 }
    28             } catch (IOException e) {
    29                 e.printStackTrace();
    30             }
    31         }
    32     }
    33     
    34     public void testWrite(String content, String path){
    35         File file = new File(path);
    36         try {
    37             FileOutputStream os = new FileOutputStream(file);
    38             byte[] byteBuffer = content.getBytes();
    39             os.write(byteBuffer, 0, byteBuffer.length);
    40             os.flush();
    41             os.close();
    42         } catch (FileNotFoundException e) {
    43             e.printStackTrace();
    44         } catch (IOException e) {
    45             e.printStackTrace();
    46         }
    47     }
    48     
    49     public static void main(String[] args) {
    50         TestIO io = new TestIO();
    51         io.testRead("src/test.txt");
    52         io.testWrite("this is a test", "src/test.txt");
    53         //io.testRead("src/test.txt");
    54     }
    55     
    56 }

    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;
    public class TestIO {public void testRead(String path){FileInputStream is = null;File file = new File(path);try {is = new FileInputStream(file);byte[] byteBuffer = new byte[is.available()];int read = 0;while((read = is.read(byteBuffer))!= -1){System.out.write(byteBuffer, 0, read);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(is!=null){is.close();}} catch (IOException e) {e.printStackTrace();}}}public void testWrite(String content, String path){File file = new File(path);try {FileOutputStream os = new FileOutputStream(file);byte[] byteBuffer = content.getBytes();os.write(byteBuffer, 0, byteBuffer.length);os.flush();os.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {TestIO io = new TestIO();io.testRead("src/test.txt");io.testWrite("this is a test", "src/test.txt");//io.testRead("src/test.txt");}}

  • 相关阅读:
    一起谈.NET技术,WPF 自定义快捷键命令(Command) 狼人:
    一起谈.NET技术,WPF 基础到企业应用系列5——WPF千年轮回2 狼人:
    一起谈.NET技术,asp.net页面中输出变量、Eval数据绑定等总结 狼人:
    单片机沉思录——再谈static
    Java平台对脚本语言支持之ScriptEngine创建方式
    [置顶] [Html] Jquery那些事
    codeforces 165E Compatible Numbers
    2013第六周上机任务【项目2 程序填空(1)】
    腾讯再否认微信收费 三大运营商态度分化
    电子钟程序
  • 原文地址:https://www.cnblogs.com/zhaochifan/p/5193629.html
Copyright © 2011-2022 走看看