JAVA如何提取.txt文件中的单词。单词格式如图所示。假设文件名是Words.txt,
如有帮助辛苦点击采纳, 谢谢~
效果:
思路:
涉及到读取文件中的内容, 需要用到io流, 使用FileInputStream读取文件内容, 将读到的内容, 输出到控制台.
另外我的数据放在了Question模块的word.txt文件中, 注意一下位置的不同, 在模块下可以使用相对路径, 其他位置建议使用绝对路径
代码:
public class Test {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(new File("Question\\word.txt"));
int len;
byte[] bytes = new byte[1024];
while ((len=fis.read(bytes))!=-1){
String str = new String(bytes,0,len);
System.out.println(str);
}
fis.close();
}
}