---
title: "java代码笔记2010-06-11:java_x_文件_字节流,创建一个一个文件目录，在该目录下创建一个文件 txt, 然后写入内容进去， 再读取文件."
author: "jayxigua"
date: "2010-06-12T08:21:41Z"
tags: ["C", "Java", "QQ", "thread", "编程"]
categories: ["编程语言"]
slug: "java-2010-06-11-java-x-txt"
draft: false
lastmod: "2010-06-12T08:22:32Z"
id: 690203
url: "https://jayxigua.iteye.com/blog/690203"
---

**File f1 = new File("E:" + File.separator + "jay-x-program"**  
    **+ File.separator + "myJ2se" + File.separator + "src"**  
    **+ File.separator + "jay\_x\_20100611\_thread\_file");**

**这个是我的路径，如果大家需要运行这个小程序的话，需要把这个路径改成自己的文件夹路径。**

 

/\*创建一个一个文件目录，  
 在该目录下创建一个文件 txt  
 然后写入内容进去，  
 再读取文件，read() 和read（byte\[] b）  
 显示读取后文件的各类信息\*/  
package jay\_x\_20100611\_thread\_file;

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.Date;

public class java\_x\_文件\_字节流 {  
 public static void main(String\[] args) {

  // File.separator的使用。  
  File f1 = new File("E:" + File.separator + "jay-x-program"  
    + File.separator + "myJ2se" + File.separator + "src"  
    + File.separator + "jay\_x\_20100611\_thread\_file");  
  System.out.println("f1是否是一个文件" + f1.isFile());  
  System.out.println("f1是否是一个目录" + f1.isDirectory());  
  System.out.println("该文件的路径" + f1.getAbsolutePath());  
  String str\[] = f1.list();  
  System.out.println("显示该文件下的所有子文件：");  
  for (int i = 0; i &lt; str.length; i++) {  
   System.out.println(str\[i]);  
  }  
  File f2 = new File(f1, "text1.txt");

  try {  
   if (!f2.exists()) {  
    f2.createNewFile();  
   } else {  
    System.out.println("该文件的路径" + f2.getAbsolutePath());  
    System.out.println("上次修改是：" + f2.lastModified());  
    System.out.println("上次修改是：" + new Date(f2.lastModified()));  
    String s = new String("将string转换成 字节数组!");  
    String s2 = new String("第二次输入");  
    byte\[] bs = s.getBytes();  
    byte bs2\[] = s2.getBytes();  
    FileOutputStream fos = new FileOutputStream(f2);  
    System.out.println("写入之前" +  
      "\\t的字节数" + f2.length());  
    fos.write(bs);  
    System.out.println("完事后的字节数" + f2.length());

    System.out  
      .println("\\n\\n\\n接下来就是读取文件内容了：" + f2.getAbsolutePath());  
    FileInputStream fis = new FileInputStream(f2);

    int b;  
     System.out.println("read():");  
     while ((b = fis.read()) != -1) {  
     char c = (char) b;  
     System.out.print(c);  
     }  
     System.out.println("测试read()的返回值：" + b);// -1  
    fos.write(bs2);  
    // 问题：fis.read()每次读了一次后会显示到下一行，不能够重复应用！  
    System.out.println("\\n\\n\\n(byte\[] b, int off, int len):");  
    byte\[] bs3 = new byte\[100];  
    int length = (int) f2.length();  
    int b2;  
    while ((b2 = fis.read(bs3, 0, length)) != -1) {  
     /*  
      * int read(byte\[] b, int off, int len) 从此输入流中将最多 len  
      * 个字节的数据读入一个字节数组中  
      \*/  
     System.out.println("(b2 = fis.read(bs3,0,length))"  
       + (b2 = fis.read(bs3, 0, length)));// -1  
     System.out.println("测试read(bs3, 0, length)的返回值：" + b2);// -1  
    }  
    // 将字节数组转换成字符串  
    String str3 = new String(bs3, 0, length);  
    System.out.println("String(bs3,0,length)结果:" + str3);  
    // String str4 = new String(bs3, 0, b2);  
    // System.out.println("String(bs3,0,b2)结果:" + str4);  
    // Exception in thread "main"  
    // java.lang.StringIndexOutOfBoundsException: String index out  
    // of range: -1  
    /*  
     * void write(byte\[] b) 将 b.length 个字节从指定字节数组写入此文件输出流中。 void  
     * write(byte\[] b, int off, int len) 将指定字节数组中从偏移量 off 开始的 len  
     * 个字节写入此文件输出流。 void write(int b) 将指定字节写入此文件输出流。  
     \*/  
   }  
  } catch (IOException e) {  
   // TODO Auto-generated catch block  
   e.printStackTrace();  
  }

 }

}

以上是我最近学习Java记录的笔记和自己参照书本和老师所讲编写的代码和注释。  
1422942883这是我的QQ，欢迎各位学习编程的同学加我好友，或者给我的个人主页留言（http://jayxigua.iteye.com/），一起讨论，学习。呵呵。