关于Java 9文本输出/输入在文件存储阵列的数据

问题描述 投票:0回答:1

我刚开始学习如何编写成文件,并从Java的OI文件中读取。现在,我可以写简单的文本文件,也可以是System.out.print使用读它。现在的问题是,我试图创建一个数组,并用我的方式学到同样将其存储在一个文件中,但是当我运行时创建该文件的程序,但也没有任何显示。请有什么我能做的事,否则,下面是我的代码示例:

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    File Veggies= new File ("C:\\Users\\user\\Desktop\\My Java Files\\Product.txt");
    if(Veggies.exists())
        System.exit(0);     

    PrintWriter output= new  PrintWriter(Veggies);
    output.println("These are the list of our products.");


    String[] VeggiesArray={"Apples","Bananas","Oranges","Avocados","Strawberries"};
    for(int i=0;i<=VeggiesArray.length;i++){
        output.println("(" + i + ")" +" "+ VeggiesArray[i]);
    }

    output.close();
}
java arrays java-9
1个回答
0
投票
    File veggies= new File (".\\Product.txt"); 

    PrintWriter output= new PrintWriter(veggies); 
    output.println("These are the list of our products."); 

    String[] veggiesArray={"Apples","Bananas","Oranges","Avocados","Strawberries"}; 
    for(int i=0;(i<veggiesArray.length);i++) { 
        output.println("(" + i + ")" +" "+ veggiesArray[i]); 
    } 
    output.close(); 

摆脱了veggies.Exists(),因为它只会让你生成文件一次,而不先删除它。

固定循环条件为MadProgrammer指出。

© www.soinside.com 2019 - 2024. All rights reserved.