zoukankan      html  css  js  c++  java
  • JAVA的文件创建

     1 package com.xia;
     2 import java.io.*;
     3 public class test2 {
     4 
     5     public static void main(String[] args) {
     6         
     7         //输出流
     8         
     9         try {
    10             FileOutputStream fos = new FileOutputStream("d:\213.txt",true); //true 允许追加写入
    11             
    12             String str = "
    大家下午好!天气很不错!";  //  
     "转义字符"
    13             
    14             String str2 = "
    心情不错";
    15             
    16             fos.write(str.getBytes()); //覆盖写入
    17             
    18             fos.close();
    19             
    20             FileInputStream fis = new FileInputStream("d:\213.txt");
    21             
    22             byte[] b = new byte[2048];
    23             
    24             int i = fis.read(b);
    25             
    26             String str1 = new String(b, 0 ,i);
    27             
    28             System.out.println("读取内容 = "+ str1);
    29             
    30             fis.close();
    31             
    32         } catch (Exception e) {
    33             
    34             e.printStackTrace();
    35         }
    36         
    37         
    38     }
    39 
    40 }

    FileOutputStream fos = new FileOutputStream("d:\213.txt",true);

    fos.write(str.getBytes()); 

    byte[] b = new byte[2048];

    int i = fis.read(b);

    String str1 = new String(b, 0 ,i);

    System.out.println("读取内容 = "+ str1);

    fis.close();

     1 package com.xia;
     2 
     3 import java.io.*;
     4 
     5 public class test3 {
     6 
     7     public static void main(String[] args) {
     8         
     9         try {
    10             
    11             FileReader fr = new FileReader("d:\213.txt");
    12             
    13             char[] c= new char[2048];
    14             
    15             int i = fr.read(c);
    16             
    17             String str = new String (c,0,i);
    18             
    19             System.out.println("读取内容 = " + str);
    20             
    21             fr.close();
    22             
    23         //写入
    24             
    25             FileWriter fw = new FileWriter("d:\213.txt", true);
    26             
    27             fw.write("
    新追加的内容");
    28             
    29             fw.close();
    30             
    31         } catch (Exception e) {
    32             
    33             e.printStackTrace();
    34         }
    35     }
    36 }

  • 相关阅读:
    mmall商城用户模块开发总结
    Hadoop伪分布式安装与配置
    1.2假设空间
    1.1基本术语
    文献笔记
    文献下载
    文献管理
    文献检索
    python:函数详解2
    python:函数详解
  • 原文地址:https://www.cnblogs.com/TENOKAWA/p/5276866.html
Copyright © 2011-2022 走看看