gpt4 book ai didi

java - 打开文件并删除java中的第一个单词

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:52 25 4
gpt4 key购买 nike

我的程序应该能够读取 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

最佳答案

试试这个方法:

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();

}

关于java - 打开文件并删除java中的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com