0
public static void main(String[] args) {

int i;
float[] npoints = new float[10];
float[] points = new float[10];
points[0]=(float) 0.3;
points[1]=(float) 0.2;
points[2]=(float) 0.4;
points[3]=(float) 0.5;
points[4]=(float) 0.6;
points[5]=(float) 0.0123;

for(i=0;i<6;i++)
{
    if(points[i]!=0.0)
    {
        npoints[i]=points[i];
        System.out.println(i+":"+points[i]);
    }

}
System.out.println(npoints[i]);
}

输出:

run:
    0:0.3
    1:0.88
    2:0.22
    3:0.95
    4:0.16
    5:0.0123
[0.95, 0.88, 0.3, 0.22, 0.16, 0.0123]
BUILD SUCCESSFUL (total time: 0 seconds)
`

我想在文本文件中打印这个输出,有什么建议吗?我是java新手

4

3 回答 3

1

您可以使用缓冲作家之类的东西

   FileWriter fw = new FileWriter(fileObj);
   BufferedWriter bw = new BufferedWriter(fw);
   bw.write(content);
   bw.close();
于 2012-09-06T20:49:31.653 回答
1

创建一个新的BufferedWriter

BufferedWriter bw = new BufferedWriter(new FileWriter("somefilename.txt"));

然后使用write方法:

bw.write(i+":"+points[i]);  // or bw.write(anything else)

完成后不要伪造关闭它BufferedWriter

bw.close();

另外,请记住从 导入适当的类java.io,并处理IOException.

相关Javadocs:

于 2012-09-06T20:53:04.930 回答
0

试试这个代码——改变这是你需要打印的第1行

import java.io.*;

public class WriteText{
   public static void main(String[] args){
       try {
           FileWriter outFile = new FileWriter(args[0]);
          PrintWriter out = new PrintWriter(outFile);

          // Also could be written as follows on one line
          // Printwriter out = new PrintWriter(new FileWriter(args[0]));

          // Write text to file
          out.println("This is line 1");
          out.println("This is line 2");
          out.print("This is line3 part 1, ");
          out.println("this is line 3 part 2");
          out.close();
      } catch (IOException e){
          e.printStackTrace();
      }
  }

}

于 2012-09-06T20:53:15.243 回答