示例:
package com.example.rfos_common_handler.demo;
import java.io.*;
/**
* @Author rfos
* @Date 2023/2/22 21:13
* @Description TODO 代码写软著
*/
public class CodeToWord1 {
private static String filePath = "F:\\Tian文档\\软件著作\\红火蚁\\views";
private static String toPath = "F:\\Tian文档\\软件著作\\红火蚁\\views.docx";
private static Integer i =0;
private static Integer j =0;
public static void main(String args[]) throws IOException {
editToWord(filePath);
System.out.println("总行数:"+j);
}
public static String editToWord(String filePath ) throws IOException {
File file = new File(filePath);
File[] files = file.listFiles();//遍历path下的文件和目录,放在File数组中
for (File file1 : files) {
if(file1.isDirectory()){
filePath = file1.toString();
editToWord(filePath);
}else {
System.out.println(file1.getName());
System.out.println(++i);
//文件名
String filename = file1.getName();
//读文件
FileInputStream fileInputStream = new FileInputStream(file1);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
//追加的方式+true
FileOutputStream fileOutputStream = new FileOutputStream(toPath,true);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
String content;
while ( (content = bufferedReader.readLine()) != null){
// String s = new String(read, 0, length);
System.out.println((++j) +":"+content);
if (content.startsWith("//")){
System.out.println(true);
continue;
}
// byte[] read = content.getBytes();
// fileOutputStream.write(read);
j++;
bufferedWriter.write(content);
//换行
bufferedWriter.newLine();
}
bufferedReader.close();
fileInputStream.close();
// bufferedWriter.flush();
fileOutputStream.close();
// bufferedWriter.close();
}
}
return null;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {
private static String path="F://LearningDirection/JavaSE/javase/readhello.txt";
private static String path1="F://LearningDirection/JavaSE/javase/readhello1.txt";
public static String readFileContent() {
IOUtils ioUtils = new IOUtils();
String filePath = path;
String reslut = ioUtils.readFile( filePath ) ;
System.out.println( reslut );
return reslut;
}
public String readFile( String filePath ){
FileInputStream fis=null;
String result = "" ;
try {
fis = new FileInputStream( filePath );
int size = fis.available() ;
byte[] array = new byte[size];
fis.read( array ) ;
result = new String(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result ;
}
public static void writeContentToFile(String[] args) {
IOUtils a2 = new IOUtils();
String filePath = path;
String content = "今天是2017/1/9,天气很好" ;
a2.writeFile( filePath , content ) ;
}
public void writeFile( String filePath , String content ){
FileOutputStream fos = null ;
try {
fos = new FileOutputStream( filePath );
byte[] array = content.getBytes() ;
fos.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
IOUtils a2 = new IOUtils();
String filePath1 = path;
String filePath2 = path1;
a2.copyFile( filePath1 , filePath2 );
}
public void copyFile( String filePath_old , String filePath_new){
FileInputStream fis=null ;
FileOutputStream fout = null ;
try {
fis = new FileInputStream( filePath_old );
int size = fis.available() ;
byte[] array = new byte[size];
fis.read( array ) ;
fout = new FileOutputStream( filePath_new ) ;
fout.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( fout != null ) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}