2014-03-27 37 views
-1

我試圖刪除文本文件中所有行的最後四個字符。假設我有domain.txt和內容:如何在java文本文件中編輯行的部分

123.com 

student.com 

tech.net 

運行到數百行。如何刪除的最後四個字符(擴展)保持:

123 

student 

tech 

+0

你嘗試過什麼? – Howli

+0

任何包含''.co.uk'',''.gov.xyz''等擴展名的行的機會? –

回答

0

我希望這有助於。

修訂

String a ="123.com"; 
    System.out.println(a.substring(0, a.lastIndexOf("."))); 
+0

我不太瞭解java,你能寫出整個代碼嗎?不要忘記它在數百行中運行。該程序應該能夠通過該文件運行並生成一個新的編輯文件 – user3468006

+0

我願意幫助不做別人的工作。對不起 – PbxMan

0

您可以如下操作:

File file = new File("file.txt"); 
    BufferedReader reader = new BufferedReader(new FileReader(file)); 
    String line = "", 
    newtext = ""; 
    while((line = reader.readLine()) != null) { 
     line=line.substring(0, line.lastIndexOf(".")) 

     newtext += line + "\n"; 
    } 
    reader.close(); 

// Now write new Content 

    FileWriter writer = new FileWriter("file.txt"); 
    writer.write(newtext); 
    writer.close(); 

不要忘了使用在try..catch

+0

我在哪裏寫主代碼?請原諒我對您的主要功能或任何其他功能中的java – user3468006

+0

的瞭解 –

相關問題