【发布时间】:2014-02-24 23:44:35
【问题描述】:
提前感谢大家。
我有几行通过文本文件输入的字符串,我想修改输出以删除每个字符串的最后两个字母。这是文本文件当前读取的内容:
你好,你好吗 酷
我很了不起
这是我正在使用的代码(来自 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();
}
}
}
代码完美运行,但我想修改输出以删除每个字符串的最后两个字母(字符串 = 每行一个)谢谢大家!
【问题讨论】:
-
我们不会为您编写程序。您向我们展示的只是您可以从文件中读取。请尝试修改文件中的字符串。
-
我不明白你是怎么知道
File、FileInputStream、BufferedInputStream、DataInputStream等的,但你不知道String。正如@Tdorno 所说,在要求我们为您做之前,您应该做更多的研究并自己尝试。