-1
應該開始說我完全是編程新手,這是在課堂上進行的最後一個練習,並且我已經在「warp 11上碰到了一堵磚牆」一。將輸出從一個文件寫入新創建的文件
編寫一個程序,該程序將讀取已經創建的文件,將內容轉換爲大寫,然後創建一個新文件並將大寫內容寫入它?這裏是我的等級和主:
import java.io.*;
import java.util.*;
public class readfile{
private Formatter z;
private Scanner x;
public void openFile(){
try{
x = new Scanner(new File ("week8lowercaseinput.txt"));
}
catch(Exception e){
System.out.println("Couldn't find file");
}
}
public void readFile(){
while(x.hasNext()){
String a = x.next();
String d = a.toUpperCase();
System.out.printf("%s ", d);
}
}
public void createFile(){
try{
z = new Formatter(new File ("WEEK8UPPERCASEOUTPUT.txt"));
}
catch(Exception e){
System.out.println("Couldn't create file.");
}
//z.Format("%s ", d);
}
public void closeFile(){
x.close();
//z.close();
}
}
主類
import java.io.*;
import java.util.*;
import java.lang.*;
class TestReadFile{
public static void main(String[] args){
readfile a = new readfile();
a.openFile();
a.readFile();
a.createFile();
a.closeFile();
}
}
我能夠打開,讀取和轉換內容。我可以創建一個新文件。唯一不起作用的是將轉換後的內容(大寫)寫入新文件。 任何幫助將不勝感激。 謝謝。
在createFile方法創建Formatter之前調用方法f.createNewFile();在創建的文件實例 – gauee
您可以發佈您實際使用的代碼嗎?你有你讀取和轉換的本地字符串。 – ChiefTwoPencils
我看不到任何寫入輸出文件的嘗試 - 有時候......更不用說「糟糕的設計」你打開一個文件,創建另一個文件,然後是'a.closeFile()' - 哪一個應該關閉?只有一個? (哪一個?)都? – John3136