Java 从文件读入字符串,但删除行上空格后的任何文本?
我有一个包含以下短语的大型文本文件:Java 从文件读入字符串,但删除行上空格后的任何文本?,java,split,bufferedreader,ignore-case,Java,Split,Bufferedreader,Ignore Case,我有一个包含以下短语的大型文本文件: citybred JJ Brestowe NNP STARS NNP NNS negative JJ NN investors NNS NNPS mountain NN 我的目标是保留每行的第一个单词,不带空格,并使它们小写。 例: 如果对上述文本进行了评估,则将返回 有什么帮助吗 当前代码: public class FileLinkList { public static void main(String args[])throws IO
citybred JJ
Brestowe NNP
STARS NNP NNS
negative JJ NN
investors NNS NNPS
mountain NN
我的目标是保留每行的第一个单词,不带空格,并使它们小写。
例:
如果对上述文本进行了评估,则将返回
有什么帮助吗
当前代码:
public class FileLinkList
{
public static void main(String args[])throws IOException{
String content = new String();
File file = new File("abc.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()){
content = sc.nextLine();
list.add(content);
}
sc.close();
} catch(FileNotFoundException fnf){
fnf.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
Collections.reverse(list);
Iterator i = list.iterator();
while (i.hasNext()) {
System.out.print("Node " + (count++) + " : ");
System.out.println(i.next());
}
}
}
公共类文件链接列表
{
公共静态void main(字符串args[])引发IOException{
字符串内容=新字符串();
File File=新文件(“abc.txt”);
LinkedList=新建LinkedList();
试一试{
Scanner sc=新扫描仪(新文件输入流(文件));
while(sc.hasNextLine()){
content=sc.nextLine();
列表。添加(内容);
}
sc.close();
}捕获(FileNotFoundException fnf){
fnf.printStackTrace();
}捕获(例外e){
e、 printStackTrace();
System.out.println(“\n程序安全终止…”);
}
收款。反向(列表);
迭代器i=list.Iterator();
while(i.hasNext()){
系统输出打印(“节点”+(计数++)+“:”;
System.out.println(i.next());
}
}
}
添加以下内容:
content = sc.nextLine();
string[] tokens = content.split(new char[] {' '}, StringSplitOptions.RemovEemptyEntries);
// You can add some validations here...
string word = tokens[0].ToLowerCase();
如果您的令牌及其POS标签之间用空格分隔:
public class FileLinkList{
public static void main(String[] args) {
BufferedReader br = null;
LinkedList<String> list = new LinkedList<String>();
String word;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("LEXICON.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
word = sCurrentLine.trim().split(" ")[0];
list.add(word.toLowerCase());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
公共类文件链接列表{
公共静态void main(字符串[]args){
BufferedReader br=null;
LinkedList=新建LinkedList();
字符串字;
试一试{
弦电流线;
br=新的BufferedReader(新的文件阅读器(“LEXICON.txt”);
而((sCurrentLine=br.readLine())!=null){
System.out.println(sCurrentLine);
word=sCurrentLine.trim().split(“”[0];
list.add(word.toLowerCase());
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
如果(br!=null)
br.close();
}捕获(IOEX异常){
例如printStackTrace();
}
}
}
}
试试这个:
public class FileLinkList {
public static void main(String args[])throws IOException{
String content = new String();
int count=1;
File file = new File("abc.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()){
content = sc.nextLine();
if (content != null && content.length() > 0)) {
list.add(content.trim().split(" ")[0].toLowerCase());
}
}
sc.close();
} catch(FileNotFoundException fnf){
fnf.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
for (String listItem : list) {
System.out.println(listItem);
}
}
}
公共类文件链接列表{
公共静态void main(字符串args[])引发IOException{
字符串内容=新字符串();
整数计数=1;
File File=新文件(“abc.txt”);
LinkedList=新建LinkedList();
试一试{
Scanner sc=新扫描仪(新文件输入流(文件));
while(sc.hasNextLine()){
content=sc.nextLine();
if(content!=null&&content.length()>0)){
list.add(content.trim().split(“”[0].toLowerCase());
}
}
sc.close();
}捕获(FileNotFoundException fnf){
fnf.printStackTrace();
}捕获(例外e){
e、 printStackTrace();
System.out.println(“\n程序安全终止…”);
}
用于(字符串列表项:列表){
System.out.println(列表项);
}
}
}
使用Apache,将文件读入字符串列表要简单得多
import org.apache.commons.io.FileUtils;
List<String> lines = FileUtils.readLines(new File("abc.txt"));
List<String firstWords = new ArrayList<>();
for (String line : lines) {
String firstWord = line.split(" ")[0].toLowerCase();
firstWords.add(firstWord);
}
import org.apache.commons.io.FileUtils;
列表行=FileUtils.readLines(新文件(“abc.txt”);
在所有评论中,我相信你的最适合我!非常感谢。抱歉:)刚刚不得不削减-d'-f1 inventory.txt | tr'[:上限:'[:下限:''
import org.apache.commons.io.FileUtils;
List<String> lines = FileUtils.readLines(new File("abc.txt"));
List<String firstWords = new ArrayList<>();
for (String line : lines) {
String firstWord = line.split(" ")[0].toLowerCase();
firstWords.add(firstWord);
}