Java基础巩固之IO

File file = new File("F:\\666.txt");
System.out.println(
file + 
file.getName() +         
file.getAbsoluteFile() + 
file.getPath()+ 
file.getAbsolutePath() + 
file.length() + 
file.getClass() + 
file.toString() + 
file.isDirectory() + 
file.isFile() + 
file.isHidden() + 
file.exists());
//file.createNewFile(); 创建文件  路径必须存在
//file.delete(); 删除文件
//file.mkdir();   新建文件夹
//file.mkdirs()  层级创建文件夹
 String[] list = file.list();
for (String fileName : list) {
System.out.println(fileName);
}
字节流读取字符
     GBK占两个字节
     UTF-8占3个字节
/**
 * 
 * 

* 过滤器 是否以.java结尾 */ public class FileFilterImpl implements FileFilter { @Override public boolean accept(File pathname) { return pathname.getName().toLowerCase().endsWith(".java"); } /** * 转换流 转码 * */ public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("F:\\666.txt"), "GBK"); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F:\\777.txt"), "UTF-8"); int read = 0; while ((read = isr.read()) != -1) { osw.write(read); } osw.flush(); osw.close(); isr.close(); } /** * 缓冲流 * */ //缓冲流去写 FileOutputStream fos = new FileOutputStream("F:\\666.txt", true); BufferedOutputStream bos = new BufferedOutputStream(fos); bos.write("哈哈哈哈".getBytes()); bos.flush(); bos.close(); fos.close(); //缓冲字节读取 FileInputStream fis = new FileInputStream("F:\\\\666.txt"); BufferedInputStream bis = new BufferedInputStream(fis); int read = 0; while ((read = bis.read()) != -1) { System.out.print((char) read); } //缓冲字符读取 FileReader fr = new FileReader("F:\\666.txt"); BufferedReader br = new BufferedReader(fr); int read = 0; while ((read = br.read()) != -1) { System.out.print((char) read); } br.close(); //缓冲字符写入 FileWriter fw = new FileWriter("F:\\666.txt", true); BufferedWriter bw = new BufferedWriter(fw); bw.write("发射点发射点发射点"); bw.flush(); bw.close(); fw.close(); fr.close();

 

你可能感兴趣的:(Java基础重新来过)