當前位置:首頁 » 編程語言 » java文件流

java文件流

發布時間: 2022-01-14 14:38:57

『壹』 java文件流的緩沖區是怎麼

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

『貳』 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文件流系統

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 中文件流和數據流的區別

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

『伍』 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 文件讀寫流

讀寫是兩個不同的分支,通常都是分開單獨使用的。
可以通過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文件流怎麼寫

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中關於文件流的讀寫(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.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的時候,就不會關閉外層流。

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

熱點內容
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:658
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:305
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:283
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:811
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:157
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:88
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:502
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:651
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:477
電腦主伺服器怎麼開機 發布:2024-09-20 07:19:07 瀏覽:728