JAVA输出到TXT文件希望能换行输出

qqasqsq 2017-10-20 02:57:37
希望
if (rgb[0] + rgb[1] + rgb[2] >= THRESOLD) {
bosS.write(( + i + " " + j + " 1 " + "\r").getBytes());
} else {
bosS.write(( + i + " " + j + " 0 " + "\r").getBytes());
}
输出结果
这段输出的结果能换行
变成这个样子



import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.imageio.ImageIO;

public class ReadColorTest {
private static final String DIR_PATH = "E:\\20 mi";//图片文件夹
private static final String TXT_PATH = "E:\\20 mi txt";//txt文件夹
private static final String COUNTER_PATH = TXT_PATH + "\\counter.txt";//计数txt
private static final int N = 100;// 阈值
private static final String THRESOLD_PATH="E:\\20 mithresold";//s txt 文件夹
private static final int THRESOLD=100;//设定的A值


/**
* 获取不带后缀名的文件名
*
* @param filename
* @return
*/
public String getname(String filename) {
int index = filename.lastIndexOf(".");
if ((index > -1) && (index < filename.length() - 1)) {
return filename.substring(0, index);
}
return filename;
}

/**
* 读取一张图片的RGB值
*
* @throws Exception
*/
public int[] getImagePixel(String image) throws Exception {
String filename = getname(image);
File fileCar = new File(TXT_PATH + "\\" + filename + ".txt");
FileOutputStream fos = new FileOutputStream(fileCar);
BufferedOutputStream bos = new BufferedOutputStream(fos);

///输出S 的输出流
File fileS=new File(THRESOLD_PATH+"\\"+filename+".txt");
FileOutputStream fosS = new FileOutputStream(fileS);
BufferedOutputStream bosS = new BufferedOutputStream(fosS);

int[] rgb = new int[3];
File file = new File(DIR_PATH + "\\" + image);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();

//计数器
int[] counter = new int[2];
counter[0] = counter[1] = 0;//初始化
System.out.println("width=" + width + ",height=" + height + ".");
bos.write(("width=" + width + ",height=" + height + ".\n").getBytes());
System.out.println("minx=" + minx + ",miniy=" + miny + ".");
bos.write(("minx=" + minx + ",miniy=" + miny + ".\n").getBytes());
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")");
bos.write(("i=" + i + ",j=" + j + ":(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")\n").getBytes());
if (rgb[0] + rgb[1] + rgb[2] >= N) {
counter[0] = counter[0] + 1;
} else {
counter[1] = counter[1] + 1;
}
//判定S
if (rgb[0] + rgb[1] + rgb[2] >= THRESOLD) {
bosS.write(( + i + " " + j + " 1 " + "\r").getBytes());
} else {
bosS.write(( + i + " " + j + " 0 " + "\r").getBytes());
}

}
}
bos.flush();
bos.close();
fos.flush();
fos.close();
bosS.flush();
bosS.close();
fosS.flush();
fosS.close();
return counter;
}

/**
* 返回屏幕色彩值
*
* @param x
* @param y
* @return
* @throws AWTException
*/
public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
System.out.println(di.width);
System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);

return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
}

/**
* 读取DIR_PATH 下面的文件名
*
* @return
*/
public String[] getImagePath() {
try {
File file = new File(DIR_PATH);
if (file.isDirectory()) {
return file.list();
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return null;
}


/**
* @param args
*/
public static void main(String[] args) throws Exception {
int x = 0;
ReadColorTest rc = new ReadColorTest();
x = rc.getScreenPixel(100, 345);
System.out.println(x + " - ");
String[] filelist = rc.getImagePath();
File counterFile = new File(COUNTER_PATH);
if (!counterFile.exists()) {
counterFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(counterFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(("图片序号,小与N的点数,大于N的点数\r\n").getBytes());
if (filelist != null && filelist.length > 0) {
for (String filename : filelist) {
int[] counter = rc.getImagePixel(filename);
bos.write((filename + "," + counter[1] + "," + counter[0] + "\r\n").getBytes());
}
}
bos.flush();
bos.close();
fos.flush();
fos.close();

}

}





...全文
652 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mozai147 2017-10-20
  • 打赏
  • 举报
回复
“\r\n”试试
Braska 2017-10-20
  • 打赏
  • 举报
回复
换行符试试用 System.getProperty("line.separator")
wcf_developer 2017-10-20
  • 打赏
  • 举报
回复
可以用newLine()试试
自由自在_Yu 2017-10-20
  • 打赏
  • 举报
回复
\r\n试试

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧