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");}}

  • 相关阅读:
    asp.net中virtual和abstract的区别分析
    .NET中的Timer类型用法详解
    类型参数的约束(C# 编程指南)T
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    jquery的$.extend和$.fn.extend作用及区别
    类型参数约束 : Controller where T : class,new()
    asp.net获取当前网址url的各种属性(文件名、参数、域名 等)的代码
    字符串一级指针的内存模型图(盲点,以前自己懵懂)
    字符串的基本操作,初始化和赋值之类的区别,和数据名是一个常量指针不可以改变和赋值(盲点众多)
    关于内存四区和指针的修改问题
  • 原文地址:https://www.cnblogs.com/zhaochifan/p/5193629.html
Copyright © 2011-2022 走看看