简体   繁体   English

System.out.println打印字符串,但是System.out.print不打印

[英]System.out.println prints the string but System.out.print does not

I am trying to store the words in a file separated by coma in a java array 我正在尝试将单词存储在java数组中用逗号分隔的文件中
The file is 该文件是

Age,Income,Student,Credit Rating,Class: Buys Computer 年龄,收入,学生,信用等级,类别:购买计算机

Youth,high,No,Fair,No 青年,高,不,一般,不

Youth,high,No,Excellent,No 青年,高,不,优秀,不

Middle aged,high,No,Excellent,No 中年,高,否,优秀,否

Senior,medium,No,Fair,Yes 高级,中级,否,一般,是

Senior,Low,Yes,Fair,Yes 高级,低,是,一般,是

Senior,Low,Yes,Excellent,No 高级,低,是,优秀,否

public class Test {
  public static void main(String args[]) throws FileNotFoundException, IOException{ 
    FileInputStream f=new FileInputStream("F:\\pr\\src\\dmexam\\inp2.txt");
    int size,nr=7,nc=5,j=0,i=0;
    char ch;
    String table[][]=new String[nr][nc];
    size=f.available();
    table[0][0]=new String();
    while(size--!=0){
         ch=(char)f.read();
         if(ch=='\n')
         {
             i++;
             if(i>=nr)
                 break;
             table[i][0]=new String();
             j=0;
             continue;

         }
         if(ch==',')
         {     
            j++;
            table[i][j]=new String();
            continue;
         }
          table[i][j]+=ch;
    }
    f.close();
    System.out.println("The given table is:::---");
    for(i=0;i<nr;i++){
       for(j=0;j<nc;j++){ 
         System.out.print("  "+table[i][j]);
         System.out.print("  ");
       }
     }
 }  
}

But the output is 但是输出是

The given table is:::--- 给定的表是::: ---

But if the for is changed like this 但是如果for这样改变

System.out.println("The given table is:::---");
for(i=0;i<nr;i++){
    for(j=0;j<nc-1;j++){ 
        System.out.print("  "+table[i][j]);

        System.out.print("  ");
    }
    System.out.println(table[i][nc-1]);
 }

The output is 输出是

The given table is:::--- Age Income Student Credit Rating Class: Buys Computer 给定的表是::: ---年龄收入学生信用等级类别:购买计算机

Youth high No Fair No 青春高不公平不

Youth high No Excellent No 青年高无优秀

Middle aged high No Excellent No 中年高无优秀否

Senior medium No Fair Yes 高级中级否一般是

Senior Low Yes Fair Yes 高级低是是一般是

Senior Low Yes Excellent No 高级低是优秀否

I want to know "why System.out.print is not workig???"... 我想知道“为什么System.out.print无法正常工作?” ...

The PrintStream that System.out uses has an internal buffer, since writing to stdout is relatively expensive -- you wouldn't necessarily want to do it for each character. System.out使用PrintStream有一个内部缓冲区,因为写入stdout的成本相对较高-您不一定要为每个字符都这样做。 That buffer is automatically flushed when you write a newline, which is why println causes the text to appear. 当您写换行符时,该缓冲区会自动刷新,这就是为什么println导致文本出现的原因。 Without that newline, your string just sits in the buffer, waiting to get flushed. 如果没有该换行符,则您的字符串仅位于缓冲区中,等待刷新。

You can force a manual flush by invoking System.out.flush() . 您可以通过调用System.out.flush()强制执行手动刷新。

Okay let me try to help you out here. 好吧,让我在这里帮助您。 So you are making your life really rough at the moment. 因此,您现在正使自己的生活变得很艰难。 Have you tried to look at different libraries like BufferedWritter/FileWritter? 您是否尝试查看过像BufferedWritter / FileWritter之类的其他库?

You can easily import these into your project using: 您可以使用以下命令轻松将它们导入到项目中:

import java.io.BufferedWritter;
import java.io.FileWritter;

It is also recommended to catch errors using the IOException library: 还建议使用IOException库捕获错误:

import java.io.IOException;

As for the separation of the words, these libraries give you tools like control over the delimiter. 至于单词的分离,这些库为您提供了诸如控制分隔符的工具。 For example we can do something like this: 例如,我们可以做这样的事情:

//this is if you are creating a new file, if not, you want true to append to an existing file
BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt", boolean false));
try
{
    // write the text string to the file
    bw.write("Youth,high,No,Fair,No");

    // creates a newline in the file
    bw.newLine();
}

// handle exceptions
catch (IOException exc)
{
    exc.printStackTrace();
}

// remember to close the file at the end
    bw.close();

Now that is for hard coding the data, but we can do this with a for loop. 现在这是用于对数据进行硬编码,但是我们可以使用for循环来完成此操作。 We can add delimiters in the function within the for loop, for example: (I am not sure how you have the data stored, but I am assuming you save it in an array. I am also assuming there will ALWAYS be 5 sets of data per line) 我们可以在for循环内的函数中添加定界符,例如:(我不确定您如何存储数据,但是我假设您将其保存在数组中。我还假设总是会有5组数据每行)

BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt", boolean false));
for (int i = 1, i <= listName.size()+1, i++) {
    if (i % 5 == 0) {
        bw.write(listName.get(i-1));
        bw.write(", ");
        bw.newLine();
    } else {
        bw.write(listName.get(i-1));
        bw.write(", ");
    }
}

This would write to the file: 这将写入文件:

Youth,high,No,Fair,No 青年,高,不,一般,不

Youth,high,No,Excellent,No 青年,高,不,优秀,不

Middle aged,high,No,Excellent,No 中年,高,否,优秀,否

Senior,medium,No,Fair,Yes 高级,中级,否,一般,是

Senior,Low,Yes,Fair,Yes 高级,低,是,一般,是

Senior,Low,Yes,Excellent,No 高级,低,是,优秀,否

This may make your life a little easier (if I am understanding your needs clearly). 这可能会使您的生活更轻松(如果我清楚地了解您的需求)。 Next time please make sure to flesh out your question more than you did. 下次,请确保充实您的问题。

DISCLAIMER: I did not test all of the code, so if you find an error please let me know and I will edit it as needed. 免责声明:我没有测试所有代码,因此,如果您发现错误,请告诉我,我将根据需要进行编辑。 If I get time I will make sure it works. 如果我有时间,我将确保它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM