Java io常用知识点

       Java io流知识点
       package myio;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

/**
* 最简单的 file的
*
* @author Administrator
*
*/
public class test1 {
@Test
public void t1() {

    String parentPath = "E:/壁纸";
    String name = "123.jpg";
    // 相对路径构建
    File src = new File(parentPath, name);
    System.out.println(src.getName());
    System.out.println(src.getPath());
    // 绝对路径
    File src2 = new File("E:/壁纸/123.jpg");
    System.out.println(src2.getName());
    System.out.println(src2.getPath());
    // 没有盘符以当前工作空间为根目录
    File src3 = new File("a.txt");
    System.out.println(src3.getName());
    // 如果是绝对路径就返回绝对路径 如果是相对路径就返回相对路径
    System.out.println(src3.getPath());
    // 绝对路径
    System.out.println(src3.getAbsolutePath());
    // 获取上一级目录
    System.out.println(src.getParent());
    System.out.println(src2.getParent());
    // 没有盘符创建的文件夹返回null
    System.out.println(src3.getParent());
}

/**
 * 判断信息
 */
@Test
public void t2() {
    File file = new File("a.txt");
    File src = new File("E:/壁纸/123.jpg");
    System.out.println("判断文件是否存在:" + file.exists());
    boolean canread = src.canRead();
    boolean canwrite = file.canWrite();
    System.out.println("canread=" + canread);
    System.out.println("canwrite=" + canwrite);
    System.out.println("是否是文件夹:" + src.isDirectory());
    if (src.isFile()) {
        System.out.println("文件");
    } else {
        // 文件夹  或者文件不存在
        if(src.isDirectory()){
            System.out.println("文件夹");
        }else{
            System.out.println("文件不存在");
        }

    }
    // 文件的字节数 只能读文件的 读不了文件夹的
    System.out.println(src.length());
}

/**
 * 创建和删除文件
 * 
 * @throws IOException
 */
@Test
public void t3() throws IOException {
    File src = new File("E:/壁纸/12345.jpg");
    if (!src.exists()) {// 不存在 则创建文件 存在 返回false
        boolean flag = src.createNewFile();
        System.out.println(flag);
    }
    // 删除文件
    boolean deleteFlag = src.delete();
    System.out.println(deleteFlag);
    // 创建临时文件
    File file = File.createTempFile("test", ".temp", new File("E:/壁纸"));
    // 退出即删除
    file.deleteOnExit();
}

}

package myio;

import java.io.File;
import java.io.FileFilter;

import org.junit.Test;

/**
* 操作目录
*
* @author Administrator
*
*/

public class test2 {
/**
* 创建文件夹
*/
@Test
public void t1() {
String path = “E:/壁纸/123456”;
File file = new File(path);
// 创建文件夹 前提是前面的父目录存在
boolean a = file.mkdir();
System.out.println(a);
File file2 = new File(“E:/壁纸/123/456/678”);
// 如果父目录链不存在 则一起创建
boolean b = file2.mkdirs();
System.out.println(b);
}

/**
 * 获取文件夹里的内容
 */
@Test
public void t2() {
    String path = "E:/壁纸";
    File file = new File(path);
    if (file.isDirectory()) {
        // 获取文件夹里所有的文件名 文件夹名也有
        String[] fileNames = file.list();
        for (String string : fileNames) {
            System.out.println(string);
        }
        System.out.println("-------分割线------");
        // 获取文件夹里的所有文件路径 文件夹路径也有
        File[] fil = file.listFiles();
        for (File f : fil) {
            System.out.println(f.getAbsolutePath());
        }

    }
}

}

package myio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

/**
* 文件的读取 第一个
*
* @author Administrator
*
*/
public class test3 {
@Test
public void t1() {
// 要读取的文件
String path = “E:/壁纸/a.txt”;
File file = new File(path);
// 选择流
InputStream ins = null;
try {
ins = new FileInputStream(file);
// 定义一个字节数组
byte[] by = new byte[1];
int len = 0;
/**
* 有点小不明白 这个地方要new String几次 能接上吗 接不上 但是能全部输出 测试用
* 真要读文本文件用objectInputstream 一行一行读
*/
int a = 0;
while ((len = ins.read(by)) != -1) {
a++;
String str = new String(by, 0, len);
System.out.println(str);
}
System.out.println(“a==” + a);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流

        if (ins != null) {
            try {
                ins.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

/**
 * 写进文件里 输出流 最简单的 测试
 * 
 * @throws IOException
 */
@Test
public void t2() throws IOException {
    String path = "E:/壁纸";
    // 本来这个文件是不存在的 先创建出来再往里面写入东西
    File file = new File(path, "a.txt");
    // 选流
    OutputStream ops = new FileOutputStream(file);
    String str = "羌笛何须怨杨柳,春风不度玉门关。";
    // 写入
    byte[] byt = str.getBytes();
    // 下面两个一样
    // ops.write(byt,0,byt.length);
    ops.write(byt);
    // 刷新一下
    ops.flush();
    // 关闭
    ops.close();
}

/**
 * 文件的拷贝
 * 
 * @throws IOException
 */
@Test
public void t3() throws IOException {
    // 源文件 必须存在不然报错
    File filea = new File("E:/壁纸/a.txt");
    // 目录文件 目录文件可以是不存在的 
    File fileb = new File("E:/壁纸/bbb.txt");

/*  // 如果文件已存在 但是是文件夹
    if (fileb.isDirectory()) {
        fileb.delete();
    }
    // 如果不存在 则创建文件
    if (!fileb.exists()) {
        // fileb.mkdirs();// 错误 这是创建文件夹
        fileb.createNewFile();// 创建文件
    }*/
    InputStream ins = new FileInputStream(filea);
    OutputStream ops = new FileOutputStream(fileb);
    /**
     * 这里设置过大  但文件过小 出现的问题不知道影响不影响
     */
    byte[] byt = new byte[1024];
    int len = 0;
    // 一个读一个写
    while ((len = ins.read(byt)) != -1) {
        ops.write(byt);

    }
    /**
     * flush写在里面好还是外面
     */
    ops.flush();
    // 关闭流
    ops.close();
    ins.close();

}

}


package myio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

/**
* 拷贝文件夹 要用到递归 测试用 没用封装成工具类 这样方便看
*
* @author Administrator
*
*/
public class test4 {

public static void main(String[] args) throws IOException {
       File src = new File("E:/壁纸");
       File dest = new File("E:/test");
    File[] fileList = src.listFiles();
    for (File files : fileList) {
        if (files.isDirectory()) {
            // 在目标文件夹中创建改路径
            dest = new File(dest, src.getName());
            // 在此处这两个方法的作用是一样的
            // dest.mkdir();
            dest.mkdirs();
            t1(files,dest);
        }else{

             files.createNewFile();
        }
        t2(files, dest);
    }
}
public static void t1(File src,File dest) throws IOException {


}

/**
 * 只拷贝单个文件 src源文件 dest 目标文件
 * 
 * @throws IOException
 */
public static void t2(File src, File dest) throws IOException {
    InputStream ins = new FileInputStream(src);
    OutputStream ops = new FileOutputStream(dest);

    int len = 0;
    byte[] byt = new byte[1024];
    while ((len = ins.read(byt)) != -1) {
        ops.write(byt);
    }
    ops.flush();
    ops.close();
    ins.close();
}

}

package myio;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;

import org.junit.Test;

/**
* 字符流操作 字符流只能处理文本文件 .txt .html
*
* @author Administrator
*
*/
public class test5 {
/**
* 字符流的读 初次测试用
*
* @throws IOException
*/
@Test
public void t1() throws IOException {
File file = new File(“E:/壁纸/a.txt”);
// 转换流 字符转字节 这个麻烦就为了使用 bufferedreader的方法
//解码设置字符集
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), “utf-8”));
// 字符流用的数组要是char数组
// 这里设置太大会出问题
/*
* char[] cha = new char[1023]; while (reader.read(cha) != -1) { String
* str = new String(cha); System.out.println(str); }
*/
// 使用包装类的新方法 一行一行读
// 字节流包装类没有提供新方法
String s = null;
while ((s = reader.readLine()) != null) {
String str = new String(s);
System.out.println(str);
}
reader.close();
}

// 字符流的单个文件的拷贝
// 测试用 字符流不会这样用 应该会用序列化吧
@Test
public void t2() throws IOException {
    File filea = new File("E:/壁纸/a.txt");
    File fileb = new File("E:/壁纸/b.txt");
    Reader reader = new FileReader(filea);
    Writer writer = new FileWriter(fileb);
    /**
     * 这里设置太大貌似有问题
     */
    int len=0;
    char[] cha = new char[1024*3];
    while ((len=reader.read(cha)) != -1) {
        //建议都如下所写 
        //上面注释掉的存在多读或者少读的问题  
        //writer.write(cha);
        writer.write(cha,0,len);
    }
    writer.flush();
    reader.close();
    writer.close();

}

}


package myio;

import java.io.UnsupportedEncodingException;

/**
* 编码与解码
*
* @author Administrator
*
*/
public class test6 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = “小程序员”;
// 编码 转成二进制
// 编码与解码的时候最好都设置字符集
byte[] byt = str.getBytes(“utf-8”);
// 解码
String s = new String(byt, “utf-8”);
System.out.println(s);
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章