指定编码格式读写文件和读取文件夹下所有文件
2015-02-03 23:05
621 查看
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import org.junit.Test; public class ReadWriteFileWithEncode { @Test public void gbkToUtf() throws Exception, Exception{ String path = "d:/src"; // readfile(path); deletefile(path); } /** * 读取某个文件夹下的所有文件 */ public static boolean readfile(String filepath) throws FileNotFoundException, IOException { try { File file = new File(filepath); if (!file.isDirectory()) { System.out.println("文件"); System.out.println("path=" + file.getPath()); System.out.println("absolutepath=" + file.getAbsolutePath()); System.out.println("name=" + file.getName()); } else if (file.isDirectory()) { System.out.println("文件夹"); String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); if (!readfile.isDirectory()) { System.out.println("path=" + readfile.getPath()); System.out.println("absolutepath=" + readfile.getAbsolutePath()); System.out.println("name=" + readfile.getName()); if(/*readfile.getName().endsWith(".java")*/true){ String content = ReadWriteFileWithEncode.read(readfile.getPath(), "gbk"); System.out .println("Content------------>"+content); String path = readfile.getPath(); if(path.contains(":")){ path="E"+path.substring(1); System.out.println("path------->"+path); } ReadWriteFileWithEncode.write(path,content, "utf-8" ); } } else if (readfile.isDirectory()) { readfile(filepath + "\\" + filelist[i]); } } } } catch (FileNotFoundException e) { System.out.println("readfile() Exception:" + e.getMessage()); } return true; } /** * 删除某个文件夹下的所有文件 */ @Test public static boolean deletefile(String filepath) throws FileNotFoundException, IOException { try { File file = new File(filepath); if (!file.isDirectory()) { System.out.println("文件"); System.out.println("path=" + file.getPath()); System.out.println("absolutepath=" + file.getAbsolutePath()); System.out.println("name=" + file.getName()); } else if (file.isDirectory()) { System.out.println("文件夹"); String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); if (!readfile.isDirectory()) { System.out.println("path=" + readfile.getPath()); System.out.println("absolutepath=" + readfile.getAbsolutePath()); System.out.println("name=" + readfile.getName()); String path = readfile.getPath(); if(path.contains(":")){ path="E"+path.substring(1); System.out.println("path------->"+path); } File file1 = new File(path); System.out.println(file1.delete()); // if(readfile.getName().endsWith(".java")){ // String content = ReadWriteFileWithEncode.read(readfile.getPath(), "gbk"); // String path = readfile.getPath(); // if(path.contains(":")){ // path="E"+path.substring(1); // System.out.println("path------->"+path); // } // ReadWriteFileWithEncode.write(path,content, "utf-8" ); // } } else if (readfile.isDirectory()) { readfile(filepath + "\\" + filelist[i]); } } } } catch (FileNotFoundException e) { System.out.println("readfile() Exception:" + e.getMessage()); } return true; } /** * 按指定格式写指定内容到指定文件中 * @param path * @param content * @param encoding * @throws IOException */ public static void write(String path, String content, String encoding) throws IOException { File file = new File(path); file.delete(); file.createNewFile(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), encoding)); writer.write(content); writer.close(); } /** * 按指定格式读指定文件中的内容 * @param path * @param encoding * @return * @throws IOException */ public static String read(String path, String encoding) throws IOException { String content = ""; File file = new File(path); BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(file), encoding)); String line = null; while ((line = reader.readLine()) != null) { content += line + "\n"; } reader.close(); return content; } public static void main(String[] args) throws IOException { // String content = "中文内容"; // String path = "c:/test.txt"; // String encoding = "utf-8"; // ReadWriteFileWithEncode.write(path, content, encoding); // System.out.println(ReadWriteFileWithEncode.read(path, encoding)); String path = "d:/src"; readfile(path); // deletefile(path); } }
相关文章推荐
- java文件读写操作指定编码格式
- php读取指定目录文件夹下所有文件夹及文件的方法
- matlab操作之--读取指定文件夹下的“指定格式”文件
- MATLAB读取指定文件夹下的所有文件
- Java指定编码格式读写文件
- 遍历读取指定文件夹下指定类型的所有文件
- java文件读写操作指定编码格式
- C#获取文件夹下指定格式的所有文件
- java找到文件夹下所有的指定格式文件并输出到txt
- C语言读取指定文件夹下面的所有文件
- C#读取指定文件夹中的所有文件
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- 文件夹扫描--获取指定文件夹下所有的指定格式的文件
- android读取指定文件夹里的所有文件
- java文件读写操作指定编码格式
- vb-复制ie临时文件夹下所有mp3文件到指定目录,并且将utf8编码转换过来
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- 遍历读取指定文件夹下指定类型的所有文件
- [安卓源代码]简单实现SD卡上遍历所有文件夹寻找指定格式文件
- Java遍历文件夹&读取指定格式的文件zUI金