2012-09-06 148 views
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]); 
} 

输出:打印输出在java中

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

我需要打印输出的文本文件,有什么建议?我是新来的Java

+4

google'java write to file' – MStodd

回答

1

您可以使用类似buffered writer

FileWriter fw = new FileWriter(fileObj); 
    BufferedWriter bw = new BufferedWriter(fw); 
    bw.write(content); 
    bw.close(); 
+0

它说没有合适的方法找到写(java.util.ArrayList ) – user1277399

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

相关的Javadoc:

+0

当我尝试使用arraylist,即bw.write(它提供了一个错误 - >没有找到合适的写入方法(java.util.ArrayList ) – user1277399

+1

是的,因为'write'方法需要一个'String' - 试试这个:'for (double d:arr)bw.write(d +“\ n”);'。 – arshajii

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

}