java輸出到文件,java輸出文件流到前端並下載

本文目錄一覽:

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如何把循環遍歷結果輸出到文本文檔?

首先,啊,我的眼睛!請學會截圖,你的這三張圖我一張都看不清!

然後我想了一下你的目的,你現在有一個學生信息數組,你是要把他們寫到一個文件里是吧,這個過程叫做數據序列化或者持久化(其實文件中保存成json串或xml的形式更容易閱讀數據和反序列化)因為看不清你的程序,所以我舉了個例子給你看下,給你些思路。

我定義一個學生類,包括姓名和分數兩個屬性:

之後在main函數中構造擁有三個學生的學生信息數組:

然後使用FileOutputStream、OutputStreamWriter、BufferedWriter完成文件的寫入:

流的使用方式我就不多說了,記住流一定要關閉,最好實在finally塊中進行,另外先打開的流後關閉。

主要看寫文件內容的部分:

其實就是循環數組,使用bufferWrite的write方法,將我們的數據按照想要的格式弄成字元串,建議使用StringBuilder來構建文件字元串內容,我這裡偷懶了直接用的+來操作,最後適時地換行。

最終生成的文件內容為:

完整main函數代碼:

public static void main(String[] args) throws Exception {

Student s1 = new Student(“張三”, 90);

Student s2 = new Student(“李四”, 59);

Student s3 = new Student(“王五”, 85);

Student[] students = new Student[]{s1, s2, s3};

String filePath = “d:\\student.txt”;

FileOutputStream fileOutputStream = null;

OutputStreamWriter outputStreamWriter = null;

BufferedWriter bufferedWriter = null;

try {

fileOutputStream = new FileOutputStream(filePath);

outputStreamWriter = new OutputStreamWriter(fileOutputStream, “UTF-8”);

bufferedWriter = new BufferedWriter(outputStreamWriter);

for (int i = 0; i students.length; i++) {

bufferedWriter.write(students[i].getName() + ” ” + students[i].getGrade());

if (i students.length – 1) {

bufferedWriter.newLine();

}

}

} finally {

if (bufferedWriter != null) {

bufferedWriter.close();

}

if (outputStreamWriter != null) {

outputStreamWriter.close();

}

if (fileOutputStream != null) {

fileOutputStream.close();

}

}

}

「java」如何將輸出結果放入文件中?

你改編一下\x0d\x0apublic class Prog50{\x0d\x0a//定義學號\x0d\x0aString[] number = new String[5];\x0d\x0apublic static void main(String[] args) throws Exception{\x0d\x0aProg50 stud = new Prog50();\x0d\x0astud.input();\x0d\x0astud.output();\x0d\x0a}\x0d\x0avoid input() throws IOException{\x0d\x0aBufferedReader br = new BufferedReader(new InputStreamReader(System.in));\x0d\x0a//錄入狀態標識\x0d\x0aboolean isRecord = true;\x0d\x0awhile(isRecord){\x0d\x0atry{\x0d\x0a for(int i=0;i System.out.print(“請輸入學號:”);\x0d\x0a number[i] = br.readLine();\x0d\x0a isRecord = false;\x0d\x0a }catch(NumberFormatException e){\x0d\x0a System.out.println(“請輸入一個數字!”);\x0d\x0a }\x0d\x0avoid output() throws IOException{\x0d\x0aFileWriter fw = new FileWriter(“E://java50//stud.txt”);\x0d\x0aBufferedWriter bw = new BufferedWriter(fw);\x0d\x0abw.write(“No. “);\x0d\x0abw.newLine();\x0d\x0afor(int i=0;i bw.write(number[i]);\x0d\x0a}\x0d\x0abw.close();\x0d\x0a}\x0d\x0a}

java輸出一個對象到文件

先創建一個文件對象:

File file = new File(“”);

if (!file.exists())

file.createNewFile();

然後創建一個文件輸出流

FileOutputStream fos = new FileOutputStream(file);

然後可以把一個對象用.toString()方法轉換成字元串。

然後再用.getBytes()轉換成字元數組。

byte[] bytes = “”.getBytes();

寫入文件:

fos.write(bytes);

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();

}

}

//簡單的一個例子,來模擬輸出

java中如何將輸出結果放入文件中

這個就需要java中的I/O流來對文件進行讀寫,舉個例子:以FileWriter類來寫文件

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

public class Test {

public static void rwFile(){

FileWriter fw = null;

try {

fw = new FileWriter(“f:\\text.txt”, true);

fw.write(“123”);//這裡向文件中輸入結果123

fw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (fw != null) {

try {

fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

rwFile();

}

}

這個代碼是向文件F盤的text.txt中輸入123

原創文章,作者:MPFTS,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/324966.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
MPFTS的頭像MPFTS
上一篇 2025-01-13 13:23
下一篇 2025-01-13 13:23

相關推薦

發表回復

登錄後才能評論