新聞中心
JAVA文件的讀寫
public class ReadFromFile {
創(chuàng)新互聯(lián)建站"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營(yíng)銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營(yíng)銷需求!創(chuàng)新互聯(lián)建站具備承接各種類型的做網(wǎng)站、網(wǎng)站建設(shè)項(xiàng)目的能力。經(jīng)過(guò)10年的努力的開(kāi)拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。
/**
* 以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀一個(gè)字節(jié):");
// 一次讀一個(gè)字節(jié)
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("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié):");
// 一次讀多個(gè)字節(jié)
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 讀入多個(gè)字節(jié)到字節(jié)數(shù)組中,byteread為一次讀入的字節(jié)數(shù)
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) {
}
}
}
}
/**
* 以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符為單位讀取文件內(nèi)容,一次讀一個(gè)字節(jié):");
// 一次讀一個(gè)字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 對(duì)于windows下,\r\n這兩個(gè)字符在一起時(shí),表示一個(gè)換行。
// 但如果這兩個(gè)字符分開(kāi)顯示時(shí),會(huì)換兩次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否則,將會(huì)多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié):");
// 一次讀多個(gè)字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 讀入多個(gè)字符到字符數(shù)組中,charread為一次讀取字符數(shù)
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("以行為單位讀取文件內(nèi)容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結(jié)束
while ((tempString = reader.readLine()) != null) {
// 顯示行號(hào)
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 隨機(jī)讀取文件內(nèi)容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("隨機(jī)讀取一段文件內(nèi)容:");
// 打開(kāi)一個(gè)隨機(jī)訪問(wèn)文件流,按只讀方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件長(zhǎng)度,字節(jié)數(shù)
long fileLength = randomFile.length();
// 讀文件的起始位置
int beginIndex = (fileLength 4) ? 4 : 0;
// 將讀文件的開(kāi)始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次讀10個(gè)字節(jié),如果文件內(nèi)容不足10個(gè)字節(jié),則讀剩下的字節(jié)。
// 將一次讀取的字節(jié)數(shù)賦給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) {
}
}
}
}
/**
* 顯示輸入流中還剩的字節(jié)數(shù)
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("當(dāng)前字節(jié)輸入流中的字節(jié)數(shù)為:" + 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文件讀寫
寫入的時(shí)候,你先讀一下之前的文件,如果有內(nèi)容,就在原有的基礎(chǔ)上+"\r\n"+加上要覆蓋的內(nèi)容。
如果沒(méi)有,直接寫入就可以了。
從文件中讀取圖片和寫入圖片到文件里的java代碼是什么?
首先導(dǎo)入各種需要的包:\x0d\x0aimport java.awt.Image;\x0d\x0aimport javax.imageio.ImageIO;\x0d\x0aimport java.io.*;\x0d\x0a讀取圖片的方法如下:\x0d\x0aImage[] array = new Image[10];\x0d\x0aImage image = ImageIO.read(new File("d:\\source.gif"));//根據(jù)你實(shí)際情況改文件路徑吧\x0d\x0aarray[0] = image;\x0d\x0a圖片讀出來(lái)了。\x0d\x0a\x0d\x0a如果你有一個(gè)Image對(duì)象,想把它寫入文件可以這樣做:\x0d\x0aBufferedImage image = ImageIO.read(new File("d:\\source.gif"));\x0d\x0a//要想保存這個(gè)對(duì)象的話你要把image聲明為BufferedImage 類型\x0d\x0aImageIO.write(image, "png", new File("f:\\test.png"));
求用java讀寫properties文件的代碼
Java代碼
package com.LY;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
public class TestMain {
// 根據(jù)key讀取value
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
String value = props.getProperty(key);
System.out.println(key + value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 讀取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
System.out.println(key + Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 寫入properties信息
public static void writeProperties(String filePath, String parameterName,
String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
// 從輸入流中讀取屬性列表(鍵和元素對(duì))
prop.load(fis);
// 調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
// 強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
// 以適合使用 load 方法加載到 Properties表中的格式,
// 將此 Properties 表中的屬性列表(鍵和元素對(duì))寫入輸出流
prop.store(fos, "Update '" + parameterName+ "' value");
} catch (IOException e) {
System.err.println("Visit " + filePath + " for updating "
+ parameterName + " value error");
}
}
public static void main(String[] args) {
readValue("info.properties", "url");
writeProperties("info.properties", "age","22");
readProperties("info.properties");
System.out.println("OK");
}
}
Java如何讀寫txt文件的代碼
有關(guān)Java如何讀寫txt文件這個(gè)問(wèn)題經(jīng)常在面試時(shí)會(huì)被問(wèn)到,不懂或不熟悉的同志們可是要記好了喲!先來(lái)看下具體實(shí)現(xiàn)吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 讀取數(shù)據(jù) */ public static void ReadDate() { String url = “e:/2.txt”; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 寫入數(shù)據(jù) */ public static void WriteDate() { try{ File file = new File(“D:/abc.txt”); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i output.write(String.valueOf(ResolveList.get(i)) + “\n”); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }
Java文件讀寫
實(shí)用的模糊(通配符)文件查找程序
1 import java.io.File;
2 import java.util.regex.Matcher;
3 import java.util.regex.Pattern;
4 import java.util.ArrayList;
5
6 /** *//**
7 * pTitle: FileService /p 8* pDescription: 獲取文件 /p 9* pCopyright: Copyright (c) 2007/p
10* pCompany: /p
11* @author not attributable
12* @version 1.0
13*/
14public class FileService {
15 public FileService() {
16 }
17
18 /** *//**
19 * 在本文件夾下查找
20 * @param s String 文件名
21 * @return File[] 找到的文件
22 */
23 public static File[] getFiles(String s)
24 {
25 return getFiles("./",s);
26 }
27
28 /** *//**
29 * 獲取文件
30 * 可以根據(jù)正則表達(dá)式查找
31 * @param dir String 文件夾名稱
32 * @param s String 查找文件名,可帶*.?進(jìn)行模糊查詢
33 * @return File[] 找到的文件
34 */
35 public static File[] getFiles(String dir,String s) {
36 //開(kāi)始的文件夾
37 File file = new File(dir);
38
39 s = s.replace('.', '#');
40 s = s.replaceAll("#", "\\\\.");
網(wǎng)頁(yè)名稱:文件讀寫java代碼,文件的讀寫java
標(biāo)題鏈接:http://fisionsoft.com.cn/article/dsiiiis.html