感谢大家提前。通过文本文件(Java)修改字符串输入中的输出
我字符串输入线通过一个文本文件,想修改输出到删除每个串的最后两个字母。这是文本文件中读取当前:
你好你怎么样 酷
我是惊人
,这是我使用(从Java-tips.org)代码
package MyProject
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class FileInput {
public static void main(String[] args) {
File file = new File("MyFile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
System.out.println(dis.readLine());
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
该代码完美工作,但我想修改输出以删除每个字符串的最后两个字母(字符串=每行一个)谢谢大家!
我们不为您编写程序。你告诉我们的是你可以从文件中读入。请在文件中显示一些修改字符串的尝试。 – Tdorno
我不明白你如何知道'File','FileInputStream','BufferedInputStream','DataInputStream'等等,但你不知道'String'。正如@Tdorno所说的,在要求我们为您做这件事之前,您应该自己做更多的研究和尝试。 –