導航:首頁 > 編程語言 > java圖片讀寫

java圖片讀寫

發布時間:2022-07-21 03:44:20

java 圖片文件的讀取和寫入問題

while(i != -1){
os.write(b, 0, b.length);
i=is.read(b, 0, b.length);
}
關鍵是這里,b僅僅是作為一個緩沖區,是可以反復使用的。
建議不要設置的太小至少1024是比較好的。

㈡ JAVA文件的讀寫

public class ReadFromFile {
/**
* 以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以位元組為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個位元組
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個位元組
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字元為單位讀取文件,常用於讀文本,數字等類型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個字元
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 對於windows下,\r\n這兩個字元在一起時,表示一個換行。
// 但如果這兩個字元分開顯示時,會換兩次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字元為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個字元
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 讀入多個字元到字元數組中,charread為一次讀取字元數
while ((charread = reader.read(tempchars)) != -1) {
// 同樣屏蔽掉\r不顯示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行為單位讀取文件,常用於讀面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結束
while ((tempString = reader.readLine()) != null) {
// 顯示行號
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 隨機讀取文件內容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("隨機讀取一段文件內容:");
// 打開一個隨機訪問文件流,按只讀方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件長度,位元組數
long fileLength = randomFile.length();
// 讀文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
// 將一次讀取的位元組數賦給byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**
* 顯示輸入流中還剩的位元組數
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("當前位元組輸入流中的位元組數為:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

㈢ Java讀寫文件的幾種方法

java讀取配置文件的幾種方法如下:
方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
方式二:採用ResourceBundle類讀取配置信息,
優點是:可以以完全限定類名的方式載入資源後,直接的讀取出來,且可以在非Web應用中讀取資源文件。缺點:只能載入類classes下面的資源文件且只能讀取.properties文件。

㈣ 怎麼用java從文件中讀取圖片和寫入圖片到文件里

首先導入各種需要的包:
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
讀取圖片的方法如下:
Image[] array = new Image[10];
Image image = ImageIO.read(new File("d:\\source.gif"));//根據你實際情況改文件路徑吧
array[0] = image;
圖片讀出來了。

如果你有一個Image對象,想把它寫入文件可以這樣做:
BufferedImage image = ImageIO.read(new File("d:\\source.gif"));
//要想保存這個對象的話你要把image聲明為BufferedImage 類型
ImageIO.write(image, "png", new File("f:\\test.png"));

㈤ java 如何讀寫文件

這個問題問的太泛泛了,讀寫文件,首先了解什麼是輸入流和輸出流,什麼是位元組流,什麼是字元流。然後創建流對象,調用其方法read or write File

㈥ 用java編寫一個圖像處理,光線補償 、

寫了很多篇關於圖像處理的文章,沒有一篇介紹Java 2D的圖像處理API,文章討論和提及的

API都是基於JDK6的,首先來看Java中如何組織一個圖像對象BufferedImage的,如圖:

㈦ java實現文件的讀寫,是叫啥

您好,你的問題,我之前好像也遇到過,以下是我原來的解決思路和方法,希望能幫助到你,若有錯誤,還望見諒!展開全部
Java.io包中包括許多類提供許多有關文件的各個方面操作。
1 輸入輸出抽象基類InputStream/OutputStream ,實現文件內容操作的基本功能函數read()、 write()、close()、skip()等;一般都是創建出其派生類對象(完成指定的特殊功能)來實現文件讀寫。在文件讀寫的編程過程中主要應該注意異常處理的技術。
2 FileInputStream/FileOutputStream:
用於本地文件讀寫(二進制格式讀寫並且是順序讀寫,讀和寫要分別創建出不同的文件流對象);
本地文件讀寫編程的基本過程為:
① 生成文件流對象(對文件讀操作時應該為FileInputStream類,而文件寫應該為FileOutputStream類);
② 調用FileInputStream或FileOutputStream類中的功能函數如read()、write(int b)等)讀寫文件內容;
③ 關閉文件(close())。
3 PipedInputStream/PipedOutputStream:
用於管道輸入輸出(將一個程序或一個線程的輸出結果直接連接到另一個程序或一個線程的輸入埠,實現兩者數據直接傳送。操作時需要連結);
4管道的連接:
方法之一是通過構造函數直接將某一個程序的輸出作為另一個程序的輸入,在定義對象時指明目標管道對象
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream(pInput);
方法之二是利用雙方類中的任一個成員函數 connect()相連接
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream();
pinput.connect(pOutput);
5 管道的輸入與輸出:
輸出管道對象調用write()成員函數輸出數據(即向管道的輸入端發送數據);而輸入管道對象調用read()成員函數可以讀起數據(即從輸出管道中獲得數據)。這主要是藉助系統所提供的緩沖機制來實現的。
6隨機文件讀寫:
RandomAccessFile類(它直接繼承於Object類而非InputStream/OutputStream類),從而可以實現讀寫文件中任何位置中的數據(只需要改變文件的讀寫位置的指針)。
隨機文件讀寫編程的基本過程為:
① 生成流對象並且指明讀寫類型;
② 移動讀寫位置;
③ 讀寫文件內容;
④ 關閉文件。

七里河團隊答疑助人,希望我的回答對你有所幫助非常感謝您的耐心觀看,如有幫助請採納,祝生活愉快!謝謝!

㈧ java io讀寫圖片無法覆蓋

你可以換種方式,建一個新的文件來保持圖片,然後把原來的圖片刪除。

閱讀全文

與java圖片讀寫相關的資料

熱點內容
文件夾怎麼做標題 瀏覽:31
騰訊雲伺服器如何防止被攻擊 瀏覽:879
六稜柱的體積演算法 瀏覽:933
淘寶什麼雲伺服器好用 瀏覽:340
pythonoa項目 瀏覽:307
android杜比音效 瀏覽:341
殺手47為什麼連接不了伺服器 瀏覽:108
靜態路徑命令 瀏覽:533
一直編譯不過怎麼辦 瀏覽:829
汽車串聯並聯演算法 瀏覽:458
助眠解壓的聲音音頻小哥哥 瀏覽:277
pythoncmd換行 瀏覽:376
linux取消行號 瀏覽:355
安卓原生系統官網是什麼 瀏覽:444
底部主圖源碼 瀏覽:878
伺服器崩了有什麼提示 瀏覽:780
遠程海康伺服器用什麼瀏覽器 瀏覽:232
解壓報紙圖片 瀏覽:956
python微信公眾號開發平台 瀏覽:895
知識付費網站java源碼 瀏覽:255