導航:首頁 > 編程語言 > java文件流

java文件流

發布時間:2022-01-17 06:50:07

⑴ 在java 中文件流和數據流的區別

文件流是通過方法可以知道長度,名稱等詳細信息的數據流。主要用於文件操作,在文件流中有自己的適用於文件操作的數據格式。而數據流是一個統稱,所有的流都可以稱為數據流。文件流屬於數據流的一種。

⑵ Java 文件流操作

利用讀取流讀取到數據然後輸出流輸出就可以了

BufferedReader bufr = new BufferedReader(new FileReader("D:\\test.txt"));

PrintWriter pw = new PrintWriter(new FileWriter("D:\\test1.txt",true);

String line = null;
while((line = bufr.readLine)!=null){
pw.println(line);
}

bufr.close;
pw.close;

⑶ java 文件讀寫流

讀寫是兩個不同的分支,通常都是分開單獨使用的。
可以通過BufferedReader 流的形式進行流緩存,之後通過readLine方法獲取到緩存的內容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此時獲取到的bre就是整個文件的緩存流
while ((str = bre.readLine())!= null) // 判斷最後一行不存在,為空結束循環
{
System.out.println(str);//原樣輸出讀到的內容
};
備註: 流用完之後必須close掉,如上面的就應該是:bre.close(),否則bre流會一直存在,直到程序運行結束。
可以通過「FileOutputStream」創建文件實例,之後過「OutputStreamWriter」流的形式進行存儲,舉例:
OutputStreamWriter pw = null;//定義一個流
pw = new OutputStreamWriter(new FileOutputStream(「D:/test.txt」),"GBK");//確認流的輸出文件和編碼格式,此過程創建了「test.txt」實例
pw.write("我是要寫入到記事本文件的內容");//將要寫入文件的內容,可以多次write
pw.close();//關閉流
備註:文件流用完之後必須及時通過close方法關閉,否則會一直處於打開狀態,直至程序停止,增加系統負擔。

⑷ JAVA文件流的緩沖區是怎麼

緩沖區其實就是個位元組數組
如果一個文件很大,比如1G
如果直接寫入內存而此時你的內存不足1G,那麼就會內存溢出。當然即使不溢出也不應該把很大的文件直接載入在內存中
這時就用到了buffer,new一個4096長度的位元組數組只需要佔用4KB的內存,通過循環讀寫就可以把1G的文件寫入目標,這樣做不僅節約了內存,而且相對來說高效

⑸ java中的文件流

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Test implements ActionListener {
String[] list1;

String[] list2;

String file1 = "/home/soft01/1.txt";//1.txt路徑

String file2 = "/home/soft01/2.txt";//2.txt路徑

JTextField jtf1;

JTextField jtf2;

int num = 3;

private boolean flag = true;

public Test() {
begin();
list1 = fileToString(file1);
list2 = fileToString(file2);
}

public void begin() {
JFrame jf = new JFrame();
jf.setLayout(new GridLayout(3, 2));
JLabel jl1 = new JLabel(" 1");
JLabel jl2 = new JLabel(" 2");
JLabel jl3 = new JLabel(" res");
jtf1 = new JTextField(8);
jtf2 = new JTextField(8);
JTextField jtf3 = new JTextField(8);
jtf3.addActionListener(this);
jf.add(jl1);
jf.add(jtf1);
jf.add(jl2);
jf.add(jtf2);
jf.add(jl3);
jf.add(jtf3);
jf.pack();
jf.setLocation(400, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public String[] fileToString(String filePath) {
String text = "";
try {
String str;
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath)));
while ((str = reader.readLine()) != null) {
text = text + str + " ";
}
} catch (Exception e) {
e.printStackTrace();
}
return text.split(" ");
}

public void actionPerformed(ActionEvent e) {
if (num == list1.length) {
num = 3;
}
if (flag) {
jtf1.setText(list1[num]);
} else {
jtf2.setText(list2[num]);
num++;
}
flag = !flag;
}

public static void main(String[] args) {
new Test();
}
}
希望分數給我,寫這個也不容易了。

⑹ 關於java文件流的小問題:下面是代碼

先開的後關,後開的先關.....
其實這么認識是錯誤的
這個是DataOutputStream的父類FilterOutputStream的源碼
public void close() throws IOException {
try {
flush();
} catch (IOException ignored) {
}
out.close();
}
其中out是傳入的ByteArrayOutputStream baos
意思就是在做dos的close時,會做父類FilterOutputStream的close。
然後會做ByteArrayOutputStream baos的close。
實際就是這樣的咯。

所以一般只需要做最外層包裝流的close,就可以關閉這個流了。
在這個樣例中,如果做了baos的close。dos和baos共用的流已經被關閉。
所以baos的close和dos的close的實際效果是一樣的。

只是習慣上會用最外層的close。
因為比如緩存流BufferedWriter bw這種,
做bw的close會把緩沖區也關閉掉。
但是用內層的流的close的時候,就不會關閉外層流。

一句話就是外層肯定調用內層,內層肯定不調用外層。

⑺ java文件流系統

FileOutputStream fos= new FileOutputStream("dos.dat");
這一行作用是讓一個流接到本地文件dos.dat文件上 准備把 '程序里的數據寫到文件里'
BufferedOutputStream bos= new BufferedOutputStream(fos);
這一行的作用是fos對象是個基礎節點流但是它的功能不強大 所以要在這個流的基礎上套接一個帶緩沖區的流BufferedOutputStream bos 它的功能很強大
DataOutputStream dos= new DataOutputStream(bos);
這一行的作用是在那個帶緩沖區的流的基礎上 再次套接一個流 這個流提供了一些很好的方法,可以writeInt,writeByte,writeLong等。

DataOutputStream這個流一般配合DataInputStream流進行一些二進制文件的讀寫操作.

⑻ 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文件流怎麼寫

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

private File fileObject;
private String savePath;
FileOutputStream fileOutputStream = new FileOutputStream("savePath");
FileInputStream fileInputStream = new FileInputStream(fileObject);
byte[] buffer = new byte[100];
int len = 0;
while ((len = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len);
}

⑽ java中如何做文件位元組輸入流

首先,我以往經驗是寫入位元組流和輸出位元組流的方式一定要一致,不然寫入的位元組流就會出錯甚至讀取不到,下面是位元組流的寫入和讀取方法:

importjava.io.*;
classTest{
publicstaticvoidmain(Stringargs[]){
FileInputStreamfis=null;
FileOutputStreamfos=null;
byte[]buffer=newbyte[100];
inttemp=0;
try{
fis=newFileInputStream("D:/wenhao/src/from.txt");
fos=newFileOutputStream("D:/wenhao/src/to.txt");
while(true){
temp=fis.read(buffer,0,buffer.length);
if(temp==-1){
break;
}
fos.write(buffer,0,temp);
}
}
catch(Exceptione){
System.out.println(e);
}
finally{
try{
fis.close();
fos.close();
}
catch(Exceptione2){
System.out.println(e2);
}
}
}
}
閱讀全文

與java文件流相關的資料

熱點內容
phpsql單引號 瀏覽:82
英雄聯盟壓縮壁紙 瀏覽:450
辦公app需要什麼伺服器 瀏覽:626
安卓伺服器怎麼獲得 瀏覽:806
空調壓縮機冷媒的作用 瀏覽:779
淘寶app是以什麼為利的 瀏覽:655
java提取圖片文字 瀏覽:922
我的世界手機版指令復制命令 瀏覽:33
java判斷字元串為數字 瀏覽:924
androidrpc框架 瀏覽:488
雲伺服器essd和ssd 瀏覽:522
家用網關的加密方式 瀏覽:1
怎麼從ppt導出pdf文件 瀏覽:971
換汽車空調壓縮機軸承 瀏覽:845
平板怎麼登錄安卓端 瀏覽:195
圖像拼接計演算法 瀏覽:255
怎麼打開飢荒伺服器的本地文件夾 瀏覽:291
usb掃描槍編程 瀏覽:673
博易大師手機app叫什麼 瀏覽:663
刮眼影盤解壓方法 瀏覽:966