2013-11-20 12 views
0

该代码读取文件中的颜色,然后写入记事本。 它工作正常,但我得到一列数字。 当我尝试更改代码以获取多列时,输出文件为空。 如果列太多,它不接受任何数据? th戴克斯java输出记事本文件不包含数据

package color1; 

import java.awt.Color; 
import java.awt.image.BufferedImage; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class color1 { 

     public static void main(String args[]) throws IOException { 
     try { 
      //read image file 
      File file1 = new File("C:\\Users\\user\\Desktop\\project\\quh2.jpg"); 
      BufferedImage image1 = ImageIO.read(file1); 

      int h=image1.getHeight(); 

      int w=image1.getWidth(); 

      FileWriter fstream = new FileWriter("C:\\Users\\user\\Desktop\\project\\pixellog1.txt"); 
      BufferedWriter out = new BufferedWriter(fstream); 

      out.write(""+h+","+w); 

      for (int x=250; x<800; x=x+10) { 

       for (int y=0; y<500; y=y+10){ 
        int c = image1.getRGB(x,y); 
        Color color = new Color(c); 
        float p = color.getRed(); 

        out.newLine(); 
        out.write(""+p); 

       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}t of numbers 
+1

你应该确保你打电话'out'“刷新”和“关闭”,以确保缓冲被刷新,并写入文件。 –

+0

与问题无关,但不需要out.write(“”+ p)中的“”;''只需'out.write(p);'将适用于基元。对于如果你看到类似“Float @ 534ab”的对象,你应该使用'.toString()'或'String.parse(p)'函数。 – DoubleDouble

+0

@colind你应该回答这个问题 – MadProgrammer

回答

0

您应该关闭您使用的资源。

在这种情况下,您的输出文件未正确关闭,因此写入的数据永远不会刷新到文件。

尝试添加这是你的外表后循环:

out.close();

+0

谢谢,现在工作很好 – davey