Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取一个文件,该文件的每一个空格都是空白的,直到它读取的地方没有更多的字符可以读取为止_Java_String_File Io_Whitespace - Fatal编程技术网

Java 读取一个文件,该文件的每一个空格都是空白的,直到它读取的地方没有更多的字符可以读取为止

Java 读取一个文件,该文件的每一个空格都是空白的,直到它读取的地方没有更多的字符可以读取为止,java,string,file-io,whitespace,Java,String,File Io,Whitespace,我必须读一个有字符串的文件,字符串之间用空格隔开。但是有多个空格,应该在swing中读取并显示到jtext区域。但我真的不知道如何使用扫描器和字符串分隔符来解决这个问题 inputfile.txt hello good hello good hello bye great hello good hello bye great hello bye great hello bye great hello bye good hello good

我必须读一个有字符串的文件,字符串之间用空格隔开。但是有多个空格,应该在swing中读取并显示到jtext区域。但我真的不知道如何使用扫描器和字符串分隔符来解决这个问题

inputfile.txt

hello good 

hello good 

hello bye 

great 

hello good 

hello bye great 

hello bye 

great 

hello bye great 

hello bye 

good 



hello 

good 

hello bye good 

hello good 

good 

great 



good 

hello good 

bye 



hello 

hello bye 

hello bye great 

hello great 

bye 

bye 

good great 

great 

great 

bye 

bye
我试过的代码

 BufferedReader br12=new BufferedReader(new FileReader("input.txt"));  
String s4;    
        while((s4=br12.readLine())!=null)
        {
            int j=1;


            StringTokenizer st11=new StringTokenizer(s4);
            while(st11.hasMoreTokens())
            {
                String bb1=st11.nextToken();
                if(j==1)
                {
                   // bw.write(bb+" ");
                    jTextArea1.append(bb1+" ");
                }
                j++;
            }
试试这个

  File file = new File("path to your file");

try {

    Scanner sc = new Scanner(file);
    String line = null;
    while (sc.hasNextLine()) {
       line = sc.nextLine();
        jTextArea1.append(line+"\n");
    }
    sc.close();
} 
catch (FileNotFoundException e) {
    e.printStackTrace();
}

发布一些代码以便我们可以帮助您。我想您想阅读上面的聊天并在JTextArea中显示,对吗?@JoshEngelsma发布了我已经尝试过的代码。\u textArea.append(line+“\n”),以便每一行都位于textArea的新行。