java怎么清空一个文件内容_如何从文本文件中删除特定内容? - java
我正在Java的SO的幫助下從事此項(xiàng)目的工作,正在讀取一個(gè)文件夾,然后將其內(nèi)容寫入文件。然后,我需要瀏覽該內(nèi)容,僅保留末尾帶有Thumbnail.jpg的圖像。
編輯:
public static final File outFile = new File(System.getProperty("user.home") + "/Desktop/output.txt");
public static void main(String[] args) throws IOException {
getFileContents();
}
public static void getFileContents() throws IOException{
System.out.print(outFile.getAbsolutePath());
PrintWriter out = new PrintWriter(outFile);
Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
//this is where I would like to happen
if (Files.isRegularFile(filePath)) // I was thinking I could use filePath.endsWith("Thumbnail.jpg")
out.println(filePath);
});
out.close();
}
參考方案
你可以這樣
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// My test file. Change to your path
File file = new File("/home/andrew/Desktop/File.txt");
if (!file.exists()) {
throw new RuntimeException("File not found");
}
try {
Scanner scanner = new Scanner(file);
//now read the file line by line...
int lineNum = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lineNum++;
// If Thumbnail.jpg is anyone where on the line
if(line.contains("Thumbnail.jpg")){
// print the line for example. You can do whatever you what with it now
System.out.println("Found item on line: " +lineNum);
}
}
} catch(FileNotFoundException e) {
//handle this
}
}
}
Java:從文件系統(tǒng)加載資源 - java
我的項(xiàng)目設(shè)定我有以下項(xiàng)目設(shè)置:\program.jar \images\logo.png 在我的代碼中,我使用相對URL "images/logo.png"引用圖像。問題如果我在目錄中使用以下命令運(yùn)行此程序:c:\projects\program_dir\bin\>java -jar program.jar 然后一切正常,Java能…Java-搜索字符串?dāng)?shù)組中的字符串 - java
在Java中,我們是否有任何方法可以發(fā)現(xiàn)特定字符串是字符串?dāng)?shù)組的一部分。我可以避免出現(xiàn)一個(gè)循環(huán)。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一個(gè)if (some condition to tell wheth…Java:線程池如何將線程映射到可運(yùn)行對象 - java
試圖繞過Java并發(fā)問題,并且很難理解線程池,線程以及它們正在執(zhí)行的可運(yùn)行“任務(wù)”之間的關(guān)系。如果我創(chuàng)建一個(gè)有10個(gè)線程的線程池,那么我是否必須將相同的任務(wù)傳遞給池中的每個(gè)線程,或者池化的線程實(shí)際上只是與任務(wù)無關(guān)的“工人無人機(jī)”可用于執(zhí)行任何任務(wù)?無論哪種方式,Executor / ExecutorService如何將正確的任務(wù)分配給正確的線程? 參考方案 …對于Java中的isDirectory和isFile,文件始終返回false - java
為什么file為isFile()方法返回false,即使它是file。當(dāng)它是目錄時(shí),它為isDirectory()返回false。難道我做錯(cuò)了什么?我測試的這些文件/目錄不存在,我需要創(chuàng)建它們,所以這就是為什么我要測試使用createFile()還是mkdir()的原因。File file = new File("C:/Users/John/Des…JAVA:字節(jié)碼和二進(jìn)制有什么區(qū)別? - java
java字節(jié)代碼(已編譯的語言,也稱為目標(biāo)代碼)與機(jī)器代碼(當(dāng)前計(jì)算機(jī)的本機(jī)代碼)之間有什么區(qū)別?我讀過一些書,他們將字節(jié)碼稱為二進(jìn)制指令,但我不知道為什么。 參考方案 字節(jié)碼是獨(dú)立于平臺的,在Windows中運(yùn)行的編譯器編譯的字節(jié)碼仍將在linux / unix / mac中運(yùn)行。機(jī)器代碼是特定于平臺的,如果在Windows x86中編譯,則它將僅在Win…
總結(jié)
以上是生活随笔為你收集整理的java怎么清空一个文件内容_如何从文本文件中删除特定内容? - java的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Win7系统补丁怎么安装?Win7系统补
- 下一篇: win7电脑连接电视(win7电脑连接电
