java 폴더 아래의 모든 파일을 읽고 줄마다 지정한 문자열 바꾸기
6877 단어 java 파일 읽기
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class ChangeFile {
public static void main(String[] args) {
try {
BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/EntNatureDistributionDAO.txt"))));//
StringBuffer strBuffer = new StringBuffer();
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();
}
}
}
적용: 서버 붕괴로 인해 파일이 분실되면 복원 후 클래스 파일은 줄마다 많은 주석을 달았다(아래와 같다)
/* */ package com.itown.iesap.starquery.dao;
/* */
/* */ import com.itown.framework.impl.ThreadContext;
/* */ import com.itown.framework.persistence.AbstractDao;
......... .....
교체하면 다음과 같습니다.
package com.itown.iesap.starquery.dao;
import com.itown.framework.impl.ThreadContext;
import com.itown.framework.persistence.AbstractDao;
......... ......
만약 당신이 또 수백 수천 개의 이런 파일을 교체한다면 폴더 아래의 모든 파일을 읽어야 한다.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class ChangeFile {
public static void main(String[] args) {
try {
//
String filepath = "D:/AOE/abc/";//
File file = new File(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 = new StringBuffer();
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();
}
}
}
이렇게 하면 더욱 명확해진다.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class ReadFile {
/**
*
* @param args
* @author
* @update 2013-6-26 1:36:31
*/
public static void main(String[] args) {
String filePath = "D:/AOE/abc/"; //
File outPath = new File("E:/AOE/abc/"); // ( )
readFolder(filePath,outPath);
}
/**
*
* @return
*/
public static void readFolder(String filePath,File outPath){
try {
//
File file = new File(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,int index,File outPath){
try{
BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//
StringBuffer strBuffer = new StringBuffer();
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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java 폴더 아래의 모든 파일을 읽고 줄마다 지정한 문자열 바꾸기적용: 서버 붕괴로 인해 파일이 분실되면 복원 후 클래스 파일은 줄마다 많은 주석을 달았다(아래와 같다) 교체하면 다음과 같습니다. 만약 당신이 또 수백 수천 개의 이런 파일을 교체한다면 폴더 아래의 모든 파일을 읽...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.