Java 写入文件行为异常
这是我的另一个 Java 问题。
请看一下这段代码:
public static void main(String[] args) {
if( args.length != 5) {
System.out.println("Error. Wrong number of params!");
System.exit(0);
}
File file = new File(args[0]);
try {
BufferedImage image = ImageIO.read(file);
FileWriter fstream = new FileWriter("output.txt");
BufferedWriter out = new BufferedWriter(fstream);
int avg = 0;
int w = image.getWidth();
int h = image.getHeight();
Rectangle r = new Rectangle();
double whiteHelp = Double.parseDouble(args[4]);
avg = (int) (avg / 1);
int startX = Integer.parseInt(args[2]);
int startY = Integer.parseInt(args[3]);
r.width = r.height = Integer.parseInt(args[1]);
for(int i = startY; i <= h - r.height; i += r.height) {
for(int j = startX; j <= w - r.width; j += r.width) {
r.x = j;
r.y = i;
avg = getTileColor(r, image, whiteHelp);
//System.out.print(avg);
out.write(avg);
}
//System.out.println();
out.write("\n");
}
out.close();
image.flush();
System.out.println("Finished parsing the image. Solving...");
} catch (Exception e) {
System.out.println("Error.");
e.printStackTrace();
}
}
在output.txt中,我只得到不可读的文本,就像打开二进制文件一样。
当 BufferedImage 和 BufferedWriter 同时实例化时是否存在一些问题?我使用相同的过程写入另一个类中的文件,其中打开一个文件进行读取,打开另一个文件进行写入,并且一切正常。
ps 如何在不破坏代码的情况下输入 < 字符???我需要将其输入为 &是吗?
谢谢
This is yet another of my Java questions.
Please take a look at this code:
public static void main(String[] args) {
if( args.length != 5) {
System.out.println("Error. Wrong number of params!");
System.exit(0);
}
File file = new File(args[0]);
try {
BufferedImage image = ImageIO.read(file);
FileWriter fstream = new FileWriter("output.txt");
BufferedWriter out = new BufferedWriter(fstream);
int avg = 0;
int w = image.getWidth();
int h = image.getHeight();
Rectangle r = new Rectangle();
double whiteHelp = Double.parseDouble(args[4]);
avg = (int) (avg / 1);
int startX = Integer.parseInt(args[2]);
int startY = Integer.parseInt(args[3]);
r.width = r.height = Integer.parseInt(args[1]);
for(int i = startY; i <= h - r.height; i += r.height) {
for(int j = startX; j <= w - r.width; j += r.width) {
r.x = j;
r.y = i;
avg = getTileColor(r, image, whiteHelp);
//System.out.print(avg);
out.write(avg);
}
//System.out.println();
out.write("\n");
}
out.close();
image.flush();
System.out.println("Finished parsing the image. Solving...");
} catch (Exception e) {
System.out.println("Error.");
e.printStackTrace();
}
}
In output.txt I only get non-readable text, as if opening binary file for example.
Is there some king of problem when BufferedImage and BufferedWriter being instantiated simultaneously? I'm using the same procedure to write to file in another class, where one file is opened for reading, and another is opened for writing, and things work just fine.
p.s. How to enter < character without breaking the code??? Do I need to enter it as & lt ;?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用
将 int avg 写入输出。
这将调用 BufferedWriter.write(int) 方法;这将写出带有 Unicode 代码点
avg
的字符。这可能不是您想要的;-)。要以十进制格式打印数字
avg
,请使用You invoke
to write the int avg to the output.
This will invoke the method
BufferedWriter.write(int)
; this will write out the character with the Unicode code pointavg
. This is probably not what you want ;-).To print the number
avg
in decimal format, use