java读取文件中的一行并输出_java读取文件夹下所有文件并替换文件每一行中指定的字符串...

importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;public classChangeFile {public static voidmain(String[] args) {try{

BufferedReader bufReader= new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/EntNatureDistributionDAO.txt"))));//数据流读取文件

StringBuffer strBuffer= newStringBuffer();

String empty= "";

String tihuan= "";for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在

tihuan = temp.substring(0, 10);

temp= temp.replace(tihuan, empty);//替换为你想要的东东

}

strBuffer.append(temp);

strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割

}

bufReader.close();

PrintWriter printWriter= new PrintWriter("E:/EntNatureDistributionDAO.txt");//替换后输出的文件位置

printWriter.write(strBuffer.toString().toCharArray());

printWriter.flush();

printWriter.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

适用:如服务器崩溃 导致文件丢失,还原后类文件在每一行的开头都加了很多注释(如下)/* */ packagecom.itown.iesap.starquery.dao;/* */

/* */ importcom.itown.framework.impl.ThreadContext;/* */ importcom.itown.framework.persistence.AbstractDao;

.........很多很多.....

替换之后就是这样的:packagecom.itown.iesap.starquery.dao;importcom.itown.framework.impl.ThreadContext;importcom.itown.framework.persistence.AbstractDao;

.........很多很多......

如果你又成百上千个这样的文件替换那就要读取文件夹下的所有文件:importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStreamReader;importjava.io.PrintWriter;public classChangeFile {public static voidmain(String[] args) {try{//读取指定文件夹下的所有文件

String filepath = "D:/AOE/abc/";//给我你的目录文件夹路径

File file = newFile(filepath);if (!file.isDirectory()) {

System.out.println("---------- 该文件不是一个目录文件 ----------");

}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]);//String path = readfile.getPath();//文件路径

String absolutepath = readfile.getAbsolutePath();//文件的绝对路径

String filename = readfile.getName();//读到的文件名

开始挨个的读取文件 BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//数据流读取文件

StringBuffer strBuffer = newStringBuffer();

String empty= "";

String tihuan= "";for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在

tihuan = temp.substring(0, 9);//这里截取多长自己改

temp = temp.replace(tihuan, empty);//替换为你想要的东东

}

strBuffer.append(temp);

strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割

}

bufReader.close();

PrintWriter printWriter= new PrintWriter("E:/ttt/"+filename);//替换后输出的文件位置(切记这里的E:/ttt 在你的本地必须有这个文件夹)

printWriter.write(strBuffer.toString().toCharArray());

printWriter.flush();

printWriter.close();

System.out.println("ok 第 " + (i+1) +" 个文件操作成功!");读取兵输出一个文件结束}

System.out.println("---------- 所有文件操作完毕 ----------");

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

这样更加清晰明了些:importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStreamReader;importjava.io.PrintWriter;public classReadFile {/*** 主方法测试

*@paramargs

*@author杜文俊

* @update 2013-6-26 下午1:36:31*/

public static voidmain(String[] args) {

String filePath= "D:/AOE/abc/"; //给我你要读取的文件夹路径

File outPath = new File("E:/AOE/abc/"); //随便给一个输出文件夹的路径(不存在也可以)

readFolder(filePath,outPath);

}/*** 读取文件夹

*@return

*/

public static voidreadFolder(String filePath,File outPath){try{//读取指定文件夹下的所有文件

File file = newFile(filePath);if (!file.isDirectory()) {

System.out.println("---------- 该文件不是一个目录文件 ----------");

}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]);//String path = readfile.getPath();//文件路径

String absolutepath = readfile.getAbsolutePath();//文件的绝对路径

String filename = readfile.getName();//读到的文件名

readFile(absolutepath,filename,i,outPath);//调用 readFile 方法读取文件夹下所有文件

}

System.out.println("---------- 所有文件操作完毕 ----------");

}

}catch(Exception e) {

e.printStackTrace();

}

}/*** 读取文件夹下的文件

*@return

*/

public static void readFile(String absolutepath,String filename,intindex,File outPath){try{

BufferedReader bufReader= new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//数据流读取文件

StringBuffer strBuffer = newStringBuffer();

String empty= "";

String tihuan= "";for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在

tihuan = temp.substring(0, 9);//这里截取多长自己改

temp = temp.replace(tihuan, empty);//替换为你想要的东东

}

strBuffer.append(temp);

strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割

}

bufReader.close();if(outPath.exists() == false){ //检查输出文件夹是否存在,若不存在先创建

outPath.mkdirs();

System.out.println("已成功创建输出文件夹:" +outPath);

}

PrintWriter printWriter= new PrintWriter(outPath+"\\"+filename);//替换后输出的文件位置(切记这里的E:/ttt 在你的本地必须有这个文件夹)

printWriter.write(strBuffer.toString().toCharArray());

printWriter.flush();

printWriter.close();

System.out.println("第 " + (index+1) +" 个文件 "+ absolutepath +" 已成功输出到 " +outPath+"\\"+filename);

}catch(Exception e){

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是基于 Apache POI 库的 Java 代码示例,可以读取指定文件夹下所有的 xlsx 文件,并逐行读取每个文件的内容: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadXlsxFiles { public static void main(String[] args) throws IOException { // 指定文件夹路径 String folderPath = "/path/to/folder"; // 获取文件夹下所有 xlsx 文件 List<File> files = getXlsxFiles(folderPath); // 逐个读取文件内容 for (File file : files) { System.out.println("Reading file: " + file.getName()); List<String[]> rows = readXlsxFile(file); // 输出一行内容 for (String[] row : rows) { for (String cell : row) { System.out.print(cell + "\t"); } System.out.println(); } } } /** * 获取指定文件夹下所有 xlsx 文件 */ public static List<File> getXlsxFiles(String folderPath) { File folder = new File(folderPath); List<File> files = new ArrayList<>(); for (File file : folder.listFiles()) { if (file.isFile() && file.getName().endsWith(".xlsx")) { files.add(file); } } return files; } /** * 读取 xlsx 文件内容 */ public static List<String[]> readXlsxFile(File file) throws IOException { List<String[]> rows = new ArrayList<>(); FileInputStream fis = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { String[] rowData = new String[row.getLastCellNum()]; for (int i = 0; i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); if (cell != null) { rowData[i] = cell.toString(); } } rows.add(rowData); } workbook.close(); fis.close(); return rows; } } ``` 在上面的代码,`getXlsxFiles` 方法用于获取指定文件夹下所有的 xlsx 文件,而 `readXlsxFile` 方法则用于读取一个 xlsx 文件的内容,返回一个字符串数组列表,其每个字符串数组代表一个行的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值