java輸出txt
⑴ java 如何輸出數據到TXT文件內
package test;
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 javax.imageio.ImageIO;
public class ReadColorTest {
/**
* 讀取一張圖片的RGB值
*
* @throws Exception
*/
public void getImagePixel(String image) throws Exception {
File fileCar = new File("D:\\car.txt");
FileOutputStream fos = new FileOutputStream(fileCar);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int[] rgb = new int[3];
File file = new File(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();
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());
}
}
}
/**
* 返回屏幕色彩值
*
* @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的值為負,經過實踐得出:加上顏色最大值就是實際顏色值。
}
/**
* @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 + " - ");
rc.getImagePixel("D:\\car.jpg");
}
}
⑵ java文件輸出流,寫到.txt文件,如何實現換行
1 實現的方式總共有3中, 1)原始的 \r\n 方式
2)BufferedWriter的newline()方法
3) System.getProperty()方法
下面使用代碼逐一展示 , 寫到.txt文件實現換行
2 使用java中的轉義符"\r\n":
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str="aaa";
str+="\r\n";
fos.write(str);
3 BufferedWriter的newline()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
BufferedWriter bw=new BufferedWriter(fos);
bw.write("你好");
bw.newline(); //實現換行
4 System.getProperty()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str = "aaa"+System.getProperty("line.separator");
fos.write(str);
⑶ 如何用java輸出txt文件
輸入無需使用位元組流,直接字元流讀取即可。
privatevoidinput(StringfileName)throwsIOException{
try(BufferedReaderreader=newBufferedReader(newFileReader(fileName))){
Stringline;
while((line=reader.readLine())!=null){
System.out.println(line);
}
}
}
同樣輸出,只要把Input換成Output;
privatevoidoutput(StringfileName,Stringcontent)throwsIOException{
try(BufferedWriterwriter=newBufferedWriter(newFileWriter(fileName))){
writer.write(content);
writer.flush();
}
}
⑷ java輸出到TXT文件時怎麼加換行
java輸出到txt的時候增加換行符的方法如下:
package
com.anjoyo.test;
import
java.io.FileWriter;
import
java.io.IOException;
public
class
TestFileWriter
{
public
static
void
main(String[]
args)
throws
IOException{
//\r\n為換行符
FileWriter
fw
=
new
FileWriter("D:\\1.txt");
//寫入第一行換行
fw.write("第一行\r\n");
//或者獲得系統換行符
String
str
=
"第二行"
+
System.getProperty("line.separator");
fw.write(str);
fw.write("第三行");
fw.close();
/*
*
windows下的文本文件換行符:\r\n
linux/unix下的文本文件換行符:\r
*
Mac下的文本文件換行符:\n
*/
}
}
⑸ java 數據輸出到txt文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestBaiKnow {
public static void main(String[] args) throws IOException {
FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));
PrintStream p = new PrintStream(fs);
p.println(100);
p.close();
}
}
//簡單的一個例子,來模擬輸出