本文目錄一覽:
- 1、java把運行結果輸出到txt
- 2、如何用JAVA生成TXT文件
- 3、如何用java輸出txt文件
- 4、JAVA 如何輸出數據到TXT文件內
- 5、JAVA 如何實現docx文檔轉換成txt?
- 6、java 數據輸出到txt文件
java把運行結果輸出到txt
這個最主要的就是萬年曆算法,網上一搜就有 輸出保存到新建的Txt文件很容易就是IO寫入操作例如:
BufferedWriter writer = new BufferedWriter(new FileWriter(txt文件路徑加路徑名,
false));// true表示追加
writer.write(要寫入的數據);
writer.close();
如何用JAVA生成TXT文件
生成TXT的方法有很多的。常用位位元組流和字符流
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class TextFileGenerator {
public static void main(String[] args) throws Exception {
method1();
method2();
}
private static void method1() throws Exception {
String txtContent = “Hello World!”;
File file = new File(“test1.txt”);
FileWriter fw = new FileWriter(file);
fw.write(txtContent);
fw.close();
}
private static void method2() throws Exception {
String txtContent = “Hello World!”;
File file = new File(“test2.txt”);
FileOutputStream fos = new FileOutputStream(file);
fos.write(txtContent.getBytes());
fos.close();
}
}
如何用java輸出txt文件
輸入無需使用位元組流,直接字符流讀取即可。
private void input(String fileName) throws IOException {
try(BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while((line=reader.readLine()) != null) {
System.out.println(line);
}
}
}
同樣輸出,只要把Input換成Output;
private void output(String fileName, String content) throws IOException{
try(BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write(content);
writer.flush();
}
}
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 如何實現docx文檔轉換成txt?
docx文檔沒辦法直接轉成txt,兩者文件格式不一樣,需要你解析docx文檔,把裏面的內容提取出來,以你想要的形式轉化成文本,通過io操作創建對應的txt文件,把文本輸出到txt文件
java 數據輸出到txt文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestBaiduKnow {
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();
}
}
//簡單的一個例子,來模擬輸出
原創文章,作者:HLUB,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/147948.html