자바 읽 기 파일 대전
package whut.filedeal;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class InOutputDemo {
public static void main(String[] args) {
InOutputDemo demo = new InOutputDemo();
demo.readDtaByCharBufferedReader();
}
//
public void readDataByChar() {
try {
File sourceFile = new File("f:\\test.txt");
FileInputStream fin = new FileInputStream(sourceFile);
InputStreamReader ris = new InputStreamReader(fin);
int cc;
while ((cc = ris.read()) > 0) {
System.out.print((char) cc);
}
ris.close();
fin.close();
} catch (Exception e) {
System.out.println(e);
e.getStackTrace();
}
}
//
public void readDataByCharArray() {
try {
File sourceFile = new File("f:\\test.txt");
FileInputStream fin = new FileInputStream(sourceFile);
InputStreamReader ris = new InputStreamReader(fin);
char[] aa = new char[10];
int readchar = 0;
while ((readchar = ris.read(aa)) > 0) {
System.out.print(new String(aa, 0, readchar));
}
ris.close();
fin.close();
} catch (Exception e) {
System.out.println(e);
e.getStackTrace();
}
}
// BufferedReader
public void readDtaByCharBufferedReader() {
try {
File sourceFile = new File("f:\\test.txt");
FileInputStream fin = new FileInputStream(sourceFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
String aa;
// readLine null
while ((aa = br.readLine()) != null) {
System.out.println(aa);
}
fin.close();
br.close();
} catch (Exception e) {
System.out.println(e);
e.getStackTrace();
}
}
// , , 、 、 。
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);
// ,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
, 。
// , 。
// , \r,
。 , 。
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) {
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.