指定编码格式读写文件和读取文件夹下所有文件
来源:互联网 发布:票据打印的软件 编辑:程序博客网 时间:2024/06/05 16:18
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 {@Testpublic 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);}}
0 0
- 指定编码格式读写文件和读取文件夹下所有文件
- 遍历读取指定文件夹下指定类型的所有文件
- 遍历读取指定文件夹下指定类型的所有文件
- MATLAB读取指定文件夹下的所有文件
- java 循环读取一个指定文件夹下的所有文件
- Java自动读取指定文件夹下的所有文件
- 读取指定文件夹下所有文件并保存
- 读取文件夹下所有文件
- java文件读写操作指定编码格式
- java文件读写操作指定编码格式
- java文件读写操作指定编码格式
- java文件读写操作指定编码格式
- Java指定编码格式读写文件
- java文件读写操作指定编码格式
- TreeView 显示指定路径下的所有文件和文件夹
- php遍历指定路径下的所有文件和文件夹
- 删除指定目录下的所有文件和文件夹
- Java删除指定路径下所有文件和文件夹
- 2014--值得开心的事情
- oops 消息 Unable to handle kernel NULL pointer dereference at virtual address
- poj3158 Kickdown
- Web Server技术选型
- Hive-函数
- 指定编码格式读写文件和读取文件夹下所有文件
- mysql主从配置
- SDUT OJ 2399 Palindrome
- JSP连接MySQL数据库
- .net 汉字转拼音
- 素数环(回溯)
- 将汉字转化成首字母序列
- Imatest 测CCM系数的方法
- 词典变位词检索系统【算法实现】