java文件流,java文件流不關閉會怎麼樣

本文目錄一覽:

Java中有幾種類型的流

在Java.io包中還有許多其他的流,主要是為了提高性能和使用方便。C/C++只能提供位元組流。Java中的流分為兩種,一種是位元組流,另一種是字元流,分別由四個抽象類來表示(每種流包括輸入和輸出兩種所以一共四個):InputStream,OutputStream,Reader,Writer。Java中其他多種多樣變化的流均是由它們派生出來的.

字元流和位元組流是根據處理數據的不同來區分的。位元組流按照8位傳輸,位元組流是最基本的,所有文件的儲存是都是位元組(byte)的儲存,在磁碟上保留的並不是文件的字元而是先把字元編碼成位元組,再儲存這些位元組到磁碟。

1.位元組流可用於任何類型的對象,包括二進位對象,而字元流只能處理字元或者字元串;

2. 位元組流提供了處理任何類型的IO操作的功能,但它不能直接處理Unicode字元,而字元流就可以。

讀文本的時候用字元流,例如txt文件。讀非文本文件的時候用位元組流,例如mp3。理論上任何文件都能夠用位元組流讀取,但當讀取的是文本數據時,為了能還原成文本你必須再經過一個轉換的工序,相對來說字元流就省了這個麻煩,可以有方法直接讀取。

字元流處理的單元為2個位元組的Unicode字元,分別操作字元、字元數組或字元串,而位元組流處理單元為1個位元組, 操作位元組和位元組數組。所以字元流是由Java虛擬機將位元組轉化為2個位元組的Unicode字元為單位的字元而成的,所以它對多國語言支持性比較好!

1.位元組流:繼承於InputStream \ OutputStream。

OutputStream提供的方法:

void write(int b):寫入一個位元組的數據

void write(byte[] buffer):將數組buffer的數據寫入流

void write(byte[] buffer,int offset,int len):從buffer[offset]開始,寫入len個位元組的數據

void flush():強制將buffer內的數據寫入流

void close():關閉流

InputStream提供的方法:

int read():讀出一個位元組的數據,如果已達文件的末端,返回值為-1

int read(byte[] buffer):讀出buffer大小的數據,返回值為實際所讀出的位元組數

int read(byte[] buffer,int offset,int len)

int available():返迴流內可供讀取的位元組數目

long skip(long n):跳過n個位元組的數據,返回值為實際所跳過的數據數

void close():關閉流

2.字元流,繼承於InputStreamReader \ OutputStreamWriter。

字元流的類:1),BufferedReader是一種過濾器(filter)(extends FilterReader)。過濾

器用來將流的數據加以處理再輸出。構造函數為:

BufferedReader(Reader in):生成一個緩衝的字元輸入流,in為一個讀取器

BufferedReader(Reader in,int size):生成一個緩衝的字元輸入流,並指定緩衝區的大小為size

public class IOStreamDemo {

public void samples() throws IOException { //1. 這是從鍵盤讀入一行數據,返回的是一個字元串

BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));

System.out.print(”Enter a line:”);

System.out.println(stdin.readLine());

//2. 這是從文件中逐行讀入數據

BufferedReader in = new BufferedReader(new FileReader(”IOStreamDemo.java”));

String s, s2 = new String();

while((s = in.readLine())!= null)

s2 += s + “\n”;

in.close();

//3. 這是從一個字元串中逐個讀入位元組

StringReader in1 = new StringReader(s2);

int c;

while((c = in1.read()) != -1)

System.out.print((char)c);

//4. 這是將一個字元串寫入文件

try {

BufferedReader in2 = new BufferedReader(new StringReader(s2));

PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(”IODemo.out”)));

int lineCount = 1;

while((s = in2.readLine()) != null )

out1.println(lineCount++ + “: ” + s);

out1.close();

} catch(EOFException e) {

System.err.println(”End of stream”);

}

} }

對於上面的例子,需要說明的有以下幾點:

1. InputStreamReader是InputStream和Reader之間的橋樑,由於System.in是位元組流,需要用它來包裝之後變為字元流供給BufferedReader使用。

3. PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(”IODemo.out”)));

這句話體現了Java輸入輸出系統的一個特點,為了達到某個目的,需要包裝好幾層。首先,輸出目的地是文件IODemo.out,所以最內層包裝的是FileWriter,建立一個輸出文件流,接下來,我們希望這個流是緩衝的,所以用BufferedWriter來包裝它以達到目的,最後,我們需要格式化輸出結果,於是將PrintWriter包在最外層。

Java流有著另一個重要的用途,那就是利用對象流對對象進行序列化。

在一個程序運行的時候,其中的變數數據是保存在內存中的,一旦程序結束這些數據將不會被保存,一種解決的辦法是將數據寫入文件,而Java中提供了一種機制,它可以將程序中的對象寫入文件,之後再從文件中把對象讀出來重新建立。這就是所謂的對象序列化。Java中引入它主要是為了RMI(Remote

Method Invocation)和Java Bean所用,不過在平時應用中,它也是很有用的一種技術。

mac系統,java編程中文件流的路徑是如何寫的

看看這個,我昨天剛寫的: import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;public class AddList {

private String filePath = “”;

private String bakPath = “”;

private String content = “”;

Scanner sc = new Scanner(System.in);

public String readFile(){

content = “”;

if (isNull(filePath)) {

System.out.println(“文件存儲路徑:”);

filePath = sc.nextLine();

}

File file = new File(filePath);

FileReader fr = null;

try {

if (file.exists()) {

fr = new FileReader(file);

char[] chars = new char[1024];

int n = 0;

while((n = fr.read(chars)) != -1){

String string = new String(chars, 0, n);

content = content + string;

}

} else {

System.out.println(“文件不存在”);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fr != null) {

try {

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return content;

}

public void writeFile(String path){

File file = new File(path);

FileOutputStream fos = null;

mkDirs(path);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void writeFile(){

if (isNull(filePath)) {

System.out.println(“文件存儲路徑:”);

filePath = sc.nextLine();

}

File file = new File(filePath);

FileOutputStream fos = null;

mkDirs(filePath);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void mkDirs(String filepath){

if (filepath.indexOf(“\\”) != -1) {

filepath = filepath.replaceAll(“\\”, “/”);

}

int n = filepath.indexOf(“//”);

String path = filepath.substring(0, n) + “//”;

filepath = filepath.substring(filepath.indexOf(“//”) + 1, filepath.length());

String[] files = filepath.split(“/”);

for (int i = 0; i files.length – 1; i++) {

path = path + files[i];

File file = new File(path);

if (!file.exists()) {

file.mkdir();

}

}

}

public void addImfor(){

System.out.println(“——–增加記錄———“);

String name = “”;

String tel = “”;

String email = “”;

content = readFile();

while(true){

System.out.println(“姓名:”);

name = sc.next();

System.out.println(“電話:”);

tel = sc.next();

System.out.println(“Email:”);

email = sc.next();

content = content + name + “” + tel + “” + email +”==”;

System.out.println(“0、Exit 1、繼續”);

int i = sc.nextInt();

if (i == 0) {

break;

}

}

writeFile();

}

public void deleteImfor(){

System.out.println(“———刪除記錄———“);

String name = “”;

String[] imfors = null;

content = readFile();

while(true){

System.out.println(“你要刪除的姓名是:”);

name = sc.next();

if (content.indexOf(name) != -1) {

imfors = content.split(“==”);

for (int i = 0; i imfors.length; i++) {

if (imfors[i].indexOf(name) != -1) {

imfors[i] = “”;

}

}

content = “”;

for (int i = 0; i imfors.length; i++) {

if (!isNull(imfors[i])) {

content = content + imfors[i] + “==”;

}

}

writeFile();

System.out.println(“刪除成功”);

} else {

System.out.println(“此人不存在”);

}

System.out.println(“0、Exit 1、繼續”);

int i = sc.nextInt();

if (i == 0) {

break;

}

}

}

public void viewAll(){

System.out.println(“———-顯示所有————“);

content = readFile();

if (!isNull(content)) {

String[] imfors = content.split(“==”);

System.out.println(“姓名\t電話\tEmail”);

for (int i = 0; i imfors.length; i++) {

String[] imfor = imfors[i].split(“”);

for (int j = 0; j imfor.length; j++) {

System.out.print(imfor[j] + “\t”);

}

System.out.println();

}

} else {

System.out.println(“暫時還沒有記錄”);

}

}

public void queryImfor(){

System.out.println(“———-查找記錄———–“);

content = readFile();

if (!isNull(content)) {

String result = “”;

String[] imfors = null;

String[] imfor = null;

String name = “”;

boolean bool = false;

while(true){

result = “”;

System.out.println(“請輸入關鍵字(按姓名查找):”);

name = sc.next();

bool = false;

if (content.indexOf(name) != -1) {

imfors = content.split(“==”);

for (int i = 0; i imfors.length; i++) {

if (imfors[i].indexOf(name) != -1) {

imfor = imfors[i].split(“”);

if (imfor[0].equals(name)) {

bool = true;

result = result + imfors[i] + “==”;

}

}

}

if (bool) {

imfors = result.split(“==”);

System.out.println(“姓名\t電話\tEmail”);

for (int i = 0; i imfors.length; i++) {

imfor = imfors[i].split(“”);

for (int j = 0; j imfor.length; j++) {

System.out.print(imfor[j] + “\t”);

}

System.out.println();

}

} else {

System.out.println(“無此人信息”);

}

} else {

System.out.println(“無此人信息”);

}

System.out.println(“0、Exit 1、繼續”);

int i = sc.nextInt();

if (i == 0) {

break;

}

}

} else {

System.out.println(“文件還沒有記錄”);

}

}

public void copy(){

System.out.println(“———-備份———–“);

content = readFile();

if (isNull(bakPath)) {

System.out.println(“備份全路徑:”);

bakPath = sc.next();

}

writeFile(bakPath);

System.out.println(“備份成功”);

}

public boolean isNull(String string){

if (null == string || “” == string || 0 == string.length()) {

return true;

} else {

return false;

}

}

public static void main(String[] args) {

AddList add = new AddList();

Scanner sc = new Scanner(System.in);

int operater = 0;

while(true){

System.out.println(“選擇功能:\n1、增加記錄 2、刪除記錄 3、顯示所有 4、查詢記錄 5、備份 6、退出”);

operater = sc.nextInt();

if (1 == operater) {

add.addImfor();

} else if (2 == operater) {

add.deleteImfor();

} else if (3 == operater) {

add.viewAll();

} else if (4 == operater) {

add.queryImfor();

} else if (5 == operater) {

add.copy();

} else if (6 == operater) {

System.out.println(“謝謝使用”);

break;

}

}

}

}

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

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

java如何讀取文件流是什麼格式

直接用fileinputstream讀文件到內存,然後用outputstream輸出到客戶端,因為是二進位流操作,源文件是什麼格式,輸出的就是什麼格式。

java為什麼要設計File和流的概念?

兩個操作不一樣。

File是對文件操作。比如說,創建文件,複製文件,刪除文件,判斷文件是否存在等等。或者說這個操作的對象是文件整體。

文件流是對文件內容的操作。比如說,讀取文件內容,寫入文件內容等等。

原創文章,作者:JLLI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/134369.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
JLLI的頭像JLLI
上一篇 2024-10-04 00:05
下一篇 2024-10-04 00:05

相關推薦

發表回復

登錄後才能評論