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

Java System.out.println打印字符串,但System.out.print不打印,java,file,printing,Java,File,Printing,我正在尝试将单词存储在一个文件中,该文件由java数组中的coma分隔 文件是 年龄、收入、学生、信用评级、班级:购买电脑 青春,高,不,公平,不 青春,高,不,优秀,不 public class Test { public static void main(String args[]) throws FileNotFoundException, IOException{ FileInputStream f=new FileInputStream("F:\\pr\\src

我正在尝试将单词存储在一个文件中,该文件由java数组中的coma分隔 文件是

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

青春,高,不,公平,不

青春,高,不,优秀,不

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("  ");
       }
     }
 }  
}
中年,高,不,优秀,不

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("  ");
       }
     }
 }  
}
高级,中级,不,一般,是

高年级,低年级,对,一般,对

高年级,低年级,是,优秀,否

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不工作???。

有一个内部缓冲区,因为写入标准输出相对昂贵-您不一定要为每个字符执行此操作。当你写换行时,缓冲区会自动刷新,这就是为什么println会导致文本出现的原因。如果没有换行符,您的字符串只会位于缓冲区中,等待刷新


您可以通过调用来强制手动刷新。

好的,让我在这里帮助您。所以你现在的生活真的很艰难。您是否尝试过查看不同的库,如BufferedWriter/FileWriter

您可以使用以下工具轻松地将其导入到项目中:

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(", ");
    }
}
这将写入文件:

青春,高,不,公平,不

青春,高,不,优秀,不

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("  ");
       }
     }
 }  
}
中年,高,不,优秀,不

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("  ");
       }
     }
 }  
}
高级,中级,不,一般,是

高年级,低年级,对,一般,对

高年级,低年级,是,优秀,否

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("  ");
       }
     }
 }  
}
如果我能清楚地理解你的需求,这可能会让你的生活更轻松一些。下一次,请确保你的问题比以前更加充实


免责声明:我没有测试所有的代码,所以如果你发现一个错误,请告诉我,我会根据需要编辑它。如果我有时间,我会确保它能正常工作。

我会马上让你停下来,告诉你为什么你想让你的生活变得更艰难,把文本作为单个字符来读,尤其是那些应该以字节而不是文本的形式来处理数据的流,因为单个字符不需要用单个字节来写?我们有读卡器将读取的数据作为文本处理。使用BufferedReader,您可以将行作为字符串读取,而无需readLine方法的帮助。还有一个Scanner,它为其他类型提供了nextLine nextInt等方法。请注意:。我在System.out.print+表[I][j]之后添加了System.out.flush;但它仍然不起作用,你能告诉我应该在哪里添加它吗@伊莎维思,它对我有用。什么不起作用?它是否没有显示任何文本或其他内容?另外,您是如何运行的?Java的哪个操作系统版本,您实际上是如何运行可执行文件的?它给出了以前的输出…我在windows 10@yshavit中使用netbeans 8.1和jdk 1.8