本文目錄一覽:
- 1、用java讀取文件流,文件進程被java獨佔,怎樣
- 2、java中文件的讀取實現,以及用到哪些類
- 3、java中關於文件流的讀寫(Writer Reader)
- 4、java如何讀取文件流是什麼格式
- 5、java輸入輸出流是怎麼讀取的呢
用java讀取文件流,文件進程被java獨佔,怎樣
可以通過BufferedReader流的形式進行流讀取,之後通過readLine方法獲取到讀取的內容。BufferedReaderbre=null;try{Stringfile=”D:/test/test.txt”;bre=newBufferedReader(newFileReader(file));//此時獲取到的bre就是整個文件的
java中文件的讀取實現,以及用到哪些類
ava.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中關於文件流的讀寫(Writer Reader)
你好 我剛剛做了個例子 方便你看
class_writer class 這個例子是從 一個文件中讀取數據然後插入了數據庫中 你可以只看讀取與插入的過程 希望能幫到你.
public class writer {
public boolean writ(String str) {
boolean success=false;
str = str.replaceAll(” “, “”).replaceAll(“\””, “”);
String[] strs = str.split(“,”);
BaseJDBC base = new BaseJDBC();
String ML = “”;
Statement stmt;
Connection conn;
ML = “‘”+strs[0].replace(” “, “”).trim() + “‘,'”
+ strs[1].replace(” “, “”).trim() + “‘,'”
+ strs[2].replace(” “, “”).trim() + “‘,'”
+ strs[3].replace(” “, “”).trim() + “‘,'”
+ strs[4].replace(” “, “”).trim()+ “‘,'”
+ strs[5].replace(” “, “”).trim()+”‘,'”
+ strs[6].replace(” “, “”).trim()+”‘”;
String query = “INSERT INTO BANK_INFO VALUES(” + ML + “)”;
if (!strs[0].equals(“參與者行號”)) {
try {
conn=base.genConn();
stmt = conn.createStatement();
int num=stmt.executeUpdate(query);
if(num==1)success=true;
stmt.close();
conn.close();
System.out.println();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
return success;
}
//class readingclass
public class reading {
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
writer w=new writer();
try {
tempString.getBytes(“utf-8”);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(!w.writ(tempString)){
System.out.println(“第”+line+”行出現異常:”+tempString);
}else{
System.out.println(“第”+line+”行初始化成功!”);
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//1927
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = “D:\\BankInfo_20110714094211.csv”;
readFileByLines(fileName);
}
java如何讀取文件流是什麼格式
直接用fileinputstream讀文件到內存,然後用outputstream輸出到客戶端,因為是二進制流操作,源文件是什麼格式,輸出的就是什麼格式。
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);
}
}
原創文章,作者:SYJQ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/134342.html