【问题标题】:System.out.println prints the string but System.out.print does notSystem.out.println 打印字符串,但 System.out.print 不打印
【发布时间】:2016-08-23 20:03:15
【问题描述】:

我正在尝试将单词存储在 java 数组中以逗号分隔的文件中
该文件是

年龄、收入、学生、信用等级、班级:购买电脑

青春,高,不,公平,不

青春,高,不,优秀,不

中年,高,不,优秀,不

高级、中级、否、一般、是

高级,低,是,一般,是

高级,低,是,优秀,否

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("  ");
       }
     }
 }  
}

但是输出是

给定的表是:::---

但是如果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]);
 }

输出是

给定的表格是:::--- 年龄收入学生信用等级:购买电脑

青年高不公平不

青春高不优秀不

中年高不优秀不

高级中等不公平是

高级 低 是 一般 是

高级 低 是 优秀 没有

我想知道“为什么 System.out.print 不工作???”...

【问题讨论】:

  • 我会阻止你并把你推荐给Stringbuilder
  • 你为什么要让你的生活更艰难,将文本作为单个字符阅读(尤其是对于应该将数据作为字节而不是文本处理的 Streams,因为不需要写入单个字符单字节)?我们有 Readers 将读取数据作为文本处理。使用BufferedReader,您可以在readLine() 方法的帮助下将行读取为字符串。还有Scanner,它为其他类型提供了nextLine()nextInt等方法(请注意:stackoverflow.com/q/13102045)。

标签: java file printing


【解决方案1】:

PrintStream that System.out uses 有一个内部缓冲区,因为写入标准输出相对昂贵——您不一定要为每个字符都这样做。当您写入换行符时,该缓冲区会自动刷新,这就是println 导致文本出现的原因。如果没有该换行符,您的字符串只会位于缓冲区中,等待刷新。

您可以通过调用 System.out.flush() 来强制手动刷新。

【讨论】:

  • 我在System.out.print(" "+table[i][j]); 之后添加了 System.out.flush() 但它仍然无法正常工作你能告诉我应该在哪里添加它吗? @yshavit
  • 嗯,它对我有用。什么不工作?它没有显示任何文本或其他内容吗?另外,你是如何运行它的——Java 的操作系统版本是什么,你实际上是如何运行可执行文件的?
  • 它给出了之前的输出...我在 windows 10 @yshavit 中使用 netbeans 8.1 和 jdk 1.8
【解决方案2】:

好的,让我试着在这里帮助你。所以你现在让你的生活变得非常艰难。您是否尝试过查看不同的库,例如 BufferedWritter/FileWritter?

您可以使用以下方法轻松地将这些导入到您的项目中:

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

还建议使用 IOException 库来捕获错误:

import java.io.IOException;

至于单词的分隔,这些库为您提供了诸如控制分隔符之类的工具。例如我们可以这样做:

//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();

现在这是对数据进行硬编码,但我们可以使用 for 循环来完成。我们可以在 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(", ");
    }
}

这将写入文件:

青春,高,不,公平,不

青春,高,不,优秀,不

中年,高,不,优秀,不

高级、中级、否、一般、是

高级,低,是,一般,是

高级,低,是,优秀,否

这可能会让您的生活更轻松(如果我清楚地了解您的需求)。下次请确保比您更充实您的问题。

免责声明:我没有测试所有代码,所以如果您发现错误,请告诉我,我会根据需要进行编辑。如果我有时间,我会确保它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    相关资源
    最近更新 更多