我的程序应该能够读取Zuess文件
看着我!
看着我!
现在看着我!
玩得开心很有趣
但是你有
知道如何。
并在删除每行中的第一个单词时写入新文件。当我运行程序时,它会删除第一个单词。
Courier;}
\cf0 \expnd0\expndtw0\kerning0
at me!\
at me!\
at me NOW!\
is fun to have fun\
you have\
know how.\
这就是它所写的内容,最后当我尝试打印文件中的内容时,我得到的是NewFile.txt而不是文件中的实际内容,有人可以帮忙。
//Program that removes the first word from every line in a file
import java.io.*;
import java.util.Scanner;
public class test {
//Main Method
public static void main (String args[])
{
//Get the system defined
String newLine = System.getProperty("line.separator");
//Name of original file
String origFileName = "zuess.RTF";
//Name of new file
String newFileName = "NewFile.txt";
//Creating the PrintWriter
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter
(new File(newFileName));
} catch (FileNotFoundException e1) {
System.out.println("Error opening the file " + origFileName);
System.exit(0);
}
//Create scanner
Scanner inputStream = null;
try {
inputStream = new Scanner
(new File(origFileName));
} catch (FileNotFoundException e1) {
System.out.println("Error opening the file " + origFileName);
System.exit(0);
}
//While there's lines in the the file
while(inputStream.hasNextLine())
{
//get line in original file
String line = inputStream.nextLine();
//split line to get words
String[] arr = line.split(" ");
for(int i = 1; i < arr.length; i++)
{
//So the data will print in the new file
outputStream.write(arr[i] + " ");
}
//Line separator
outputStream.write(newLine);
}
//Close Stream
outputStream.close();
inputStream.close();
//catch exception
try
{
inputStream = new Scanner( new File (newFileName));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + newFileName);
System.exit(0);
}
System.out.println("The file " + newFileName + " Contains the following lines: ");
while (inputStream.hasNextLine())
{
String text = inputStream.nextLine();
System.out.println(newFileName);
}
inputStream.close();
}//End main
}//end class
答案 0 :(得分:0)
尝试这种方式:
public static void main (String args[]) throws IOException {
//Get the system defined
String newLine = System.getProperty("line.separator");
//Name of original file
String origFileName = "new.txt";
//Name of new file
String newFileName = "NewFile.txt";
//Creating the PrintWriter
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter
(new File(newFileName));
} catch (FileNotFoundException e1) {
System.out.println("Error opening the file " + origFileName);
System.exit(0);
}
//Opening origin file and getting all lines
Path pathA = Paths.get(origFileName);
List<String> file = Files.readAllLines(pathA);
//for every line from origin file
for ( String line: file) {
//split line to get words
String[] arr = line.split(" ");
for(int i = 1; i < arr.length; i++)
{
//So the data will print in the new file
outputStream.write(arr[i] + " ");
}
//Line separator
outputStream.write(newLine);
}
//Close Stream
outputStream.close();
}
答案 1 :(得分:0)
在这里你试试这个:它正在使用版本&#34; zuess.txt&#34;不是&#34; zuess.rtf&#34;
endIndex