Java outputting to text file?

后端 未结 5 696
悲哀的现实
悲哀的现实 2021-01-28 06:59

So I am having troubles printing to output. I understand the concept, but when it comes to this problem its kinda weird. I\'ve tried different print lines and all of them give m

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 07:53

    Try this :

    public void text() {
            try {
                PrintWriter output = new PrintWriter("test.txt");
                output.println(data[0]);
                for (int i = 0; i < data.length; i++) {
                    if (i != (data.length - 1)) {
                        output.printf("%d, ", data[i]);
                    } else {
                        output.printf("%d", data[i]);
                    }
                    output.flush();
                }
            } catch (Exception ex) {
    
            }
    
        }
    

提交回复
热议问题