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);
}
}
å¤å¶ä»£ç
5ãå°å
容追å å°æ件尾é¨
public class AppendToFile {
/**
* Aæ¹æ³è¿½å æ件ï¼ä½¿ç¨RandomAccessFile
*/
public static void appendMethodA(String fileName, String content) {
try {
// æå¼ä¸ä¸ªéæºè®¿é®æ件æµï¼æ读åæ¹å¼
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// æ件é¿åº¦ï¼åèæ°
long fileLength = randomFile.length();
//å°åæ件æé移å°æ件尾ã
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Bæ¹æ³è¿½å æ件ï¼ä½¿ç¨FileWriter
*/
public static void appendMethodB(String fileName, String content) {
try {
//æå¼ä¸ä¸ªåæ件å¨ï¼æé å½æ°ä¸ç第äºä¸ªåæ°true表示以追å å½¢å¼åæ件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
String content = "new append!";
//ææ¹æ³A追å æ件
AppendToFile.appendMethodA(fileName, content);
AppendToFile.appendMethodA(fileName, "append end. \n");
//æ¾ç¤ºæ件å
容
ReadFromFile.readFileByLines(fileName);
//ææ¹æ³B追å æ件
AppendToFile.appendMethodB(fileName, content);
AppendToFile.appendMethodB(fileName, "append end. \n");
//æ¾ç¤ºæ件å
容
ReadFromFile.readFileByLines(fileName);
}
}
温馨提示:答案为网友推荐,仅供参考