/**新建一個類把下面代碼放進(jìn)去,注意要設(shè)置basePath(你要讀取的文件夾),讀取和寫入的方法也都寫好了.你可以根據(jù)自己的需求掉用就行了**/
臨汾ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
static String basePath="/home/csvDir";
/**
* 查找文件夾下所有符合csv的文件
*
* @param dir 要查找的文件夾對象
* */
public static void findFile(File dir) throws IOException{
File[] dirFiles = dir.listFiles();
for(File temp : dirFiles){
if(!temp.isFile()){
findFile(temp);
}
//查找指定的文件
if(temp.isFile() temp.getAbsolutePath().endsWith(".txt") ){
System.out.println(temp.isFile() + " " + temp.getAbsolutePath());
readFileContent(temp);
}
}
}
/**
* @param file 要讀取的文件對象
* @return 返回文件的內(nèi)容
* */
public static String readFileContent(File file) throws IOException{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
while(br.ready()){
sb.append(br.readLine());
}
System.out.println(sb.toString());
return sb.toString();
}
/**
* @param file 要寫入的文件對象
* @param content 要寫入的文件內(nèi)容
* */
public static void writeFileContent(File file,String content) throws IOException{
FileWriter fw = new FileWriter(file);
fw.write(content);
fw.flush();
fw.close();
}
public static void main(String[] args) {
try {
findFile(new File(basePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Test {
public static void main(String[] args) {
try
{
FileInputStream fr=new FileInputStream("test/123.txt");
InputStreamReader af=new InputStreamReader(fr);
BufferedReader bf=new BufferedReader(af);
FileOutputStream fo=new FileOutputStream("test/456.doc");
OutputStreamWriter osw=new OutputStreamWriter(fo);
BufferedWriter bw=new BufferedWriter(osw);
String line=bf.readLine();
while(line!=null)
{
System.out.println(line);
bw.write(line);
bw.newLine();
bw.flush();
line=bf.readLine();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
test/123.txt里的內(nèi)容寫到test/456.doc下了,在你的java src中建一個test文件夾,這里是相對路徑
要清空,最簡單的方法就是新建一個文本名字與要清空的名字一樣就好,直接默認(rèn)就替代了。
分享名稱:java代碼如何梳理文檔 java代碼在哪里編寫
分享鏈接:http://www.rwnh.cn/article18/ddehhgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、域名注冊、ChatGPT、軟件開發(fā)、電子商務(wù)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)