Ⅰ java 如何通過 字元流或者位元組流 將一個txt文檔 倒敘 輸出 到另一個文檔
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.StringReader;
import java.io.StringWriter;
public class ReverseText {
public static void main(String[] args) throws Exception {
String from="c:/1.txt"; //源文件
String to="c:/2.txt"; //反轉後的新文件
FileReader reader = new FileReader(from);
FileWriter writer = new FileWriter(to);
StringWriter sWriter = new StringWriter();
int readed = reader.read();
while(readed!=-1){
//writer.write(readed);
sWriter.write(readed);
readed = reader.read();
}
StringBuffer buffer = sWriter.getBuffer();
for(int i= buffer.length()-1;i>=0;i--){
writer.write(buffer.charAt(i));
}
writer.close();
sWriter.close();
reader.close();
}
}
Ⅱ java中如何將流轉換成文件類型
我查了一下,File的api 這個File的構造方法的介紹。。。
File(File parent, String child)
根據 parent 抽象路徑名和 child 路徑名字元串創建一個新 File 實例。
File(String pathname)
通過將給定路徑名字元串轉換成抽象路徑名來創建一個新 File 實例。
File(String parent, String child)
根據 parent 路徑名字元串和 child 路徑名字元串創建一個新 File 實例。
File(URI uri)
通過將給定的 file: URI 轉換成一個抽象路徑名來創建一個新的 File 實例。
也就意味著,如果,file這種類型,就必須有一個路徑。
那,能不能在內存中虛擬一個File file呢?
File f = new File("/1.txt");
假如這樣,那麼,一旦,你開始往這個file裡面開流寫內容。只有兩種情況可能發生,一種是找不到文件,拋異常。另外一種可能是,直接create了一個文件出來,並且寫進去這個文件~~
所以,如果是這樣的情況,就很郁悶。
那麼,在user對象中,如果非要放File文件類型格式,那麼,就寫到一個臨時文件里吧。等用完之後刪除。
如果該成byte[] 或者別的內容,如果你要用數據,其實會更加方便,不用開流從文件裡面讀取,而是直接從這個數組裡面讀就是了。
所以,建議把這個User裡面的文件變成byte[] 。
一點淺見~~
另祝節日愉快~~
Ⅲ java 位元組流,字元流,字元串之間怎樣轉換
這個問題要分2個部分
位元組流和字元流之間的轉換
位元組流轉換成字元流可以用InputSteamReader/OutputStreamWriter相互轉換
2.怎麼把字元串轉為流.
下面的慧猜散程序可以理解把字元串line
轉為流前氏輸出到兆唯aaa.txt中去
FileOutputStream
fos
=
null;
fos
=
new
FileOutputStream("C:\\aaa.txt");
String
line="This
is
1
line";
fos.write(line.getBytes());
fos.close();
從你提的問題我感覺你對流的理解比較模糊,提的問題也模稜兩可的,建議你系統的理解下IO的概念.
Ⅳ java的幾種IO流讀取文件方式
一、超類:
位元組流: InputStream(讀入流) OutputStream(寫出流)
字元流: Reader(字元 讀入流) Writer (字元寫出流)
二、文件操作流
位元組流: FileInputStream ,FileOutputStream
字元流: FileReader, FileWriter(用法與位元組流基本相同,不寫)
//1.指定要讀 的文件目錄及名稱
File file =new File("文件路徑");
//2.創建文件讀入流對象
FileInputStream fis =new FileInputStream(file);
//3.定義結束標志,可用位元組數組讀取
int i =0 ;
while((i = fis.read())!=-1){
//i 就是從文件中讀取的位元組,讀完後返回-1
}
//4.關閉流
fis.close();
//5.處理異常
//1.指定要寫到的文件目錄及名稱
File file =new File("文件路徑");
//2.創建文件讀入流對象
FileOutputStream fos =new FileOutputStream(file);
//3.定義結束標志
fos.write(要寫出的位元組或者位元組數組);
//4.刷新和關閉流
fos.flush();
fos.close();
//5.處理異常
三、緩沖流:
位元組緩沖流: BufferedInputStream,BufferedOutputStream
字元緩沖流:BufferedReader ,BufferedWriter
緩沖流是對流的操作的功能的加強,提高了數據的讀寫效率。既然緩沖流是對流的功能和讀寫效率的加強和提高,所以在創建緩沖流的對象時應該要傳入要加強的流對象。
//1.指定要讀 的文件目錄及名稱
File file =new File("文件路徑");
//2.創建文件讀入流對象
FileInputStream fis =new FileInputStream(file);
//3.創建緩沖流對象加強fis功能
BufferedInputStream bis =new BufferedInputStream(fis);
//4.定義結束標志,可用位元組數組讀取
int i =0 ;
while((i = bis.read())!=-1){
//i 就是從文件中讀取的位元組,讀完後返回-1
}
//5.關閉流
bis.close();
//6.處理異常
//1.指定要寫到的文件目錄及名稱
File file =new File("文件路徑");
//2.創建文件讀入流對象
FileOutputStream fos =new FileOutputStream(file);
//3.創建緩沖流對象加強fos功能
BufferedOutputStream bos=new BufferedOutputStream(fos);
//4.向流中寫入數據
bos.write(要寫出的位元組或者位元組數組);
//5.刷新和關閉流
bos.flush();
bos.close();
//6.處理異常
四、對象流
ObjectInputStream ,ObjectOutputStream
不同於以上兩種類型的流這里只能用位元組對對象進行操作原因可以看上篇的編碼表比照原理
ObjectOutputStream對象的序列化:
將java程序中的對象寫到本地磁碟里用ObjectOutputStream
eg:將Person類的對象序列化到磁碟
創建Person類
注1:此類要實現Serializable介面,此介面為標志性介面
注2:此類要有無參的構造函數
注3:一旦序列化此類不能再修改
class Person implements Serializable{
public Person(){}
}
2.創建對象流對象
註:要增強功能可以將傳入文件緩沖流
ObjectOutputStream oos =new ObjectOutputStream(
new FileOutputStream(new File("文件路徑")));
3.寫入對象 ,一般會將對象用集合存儲起來然後直接將集合寫入文件
List<Person> list =new ArrayList<>();
list.add(new Person());
...(可以添加多個)
oos.writeObject(list);
4.關閉流,處理異常
oos.flush();
oos.close();
五、轉換流:
這類流是用於將字元轉換為位元組輸入輸出,用於操作字元文件,屬於字元流的子類,所以後綴為reader,writer;前綴inputstream,outputstream;
注 :要傳入位元組流作為參賽
InputStreamReader: 字元轉換輸出流
OutputStreamWriter:字元轉換輸入流
//1.獲取鍵盤輸入的位元組流對象
inInputStream in =Stream.in;
/*2.用轉換流將位元組流對象轉換為字元流對象,方便調用字元緩沖流的readeLine()方法*/
InputStreamReader isr =new InputStreamReader(in);
/*5.創建字元轉換輸出流對象osw,方便把輸入的字元流轉換為位元組輸出到本地文件。*/
OutputStreamWriter osw =new OutputStreamWriter(new
FileOutputStream(new File("文件名")));
/*3.現在isr是字元流,可以作為參數傳入字元緩沖流中*/
BufferedReader br =new BufferedReader(isr);
/*4.可以調用字元緩沖流br的readLine()方法度一行輸入文本*/
String line =null;
while((line =br.readLine()){
osw.write(line);//osw是字元流對象,可以直接操作字元串
}
註:InputStreamReader isr =new InputStreamReader(new "各種類型的位元組輸入流都行即是:後綴為InputStream就行");
OutputStreamWriter osw =new OutputStreamWriter(new
"後綴為OutputStream就行");
六、區別記憶
1.對象流是可以讀寫幾乎所有類型的只要是對象就行,而位元組字元流,只能讀寫單個位元組字元或者位元組字元數組,以上沒有讀寫位元組字元數組的;注意對象流只有位元組流!
2.字元和位元組循環讀入的結束條件int i=0; (i =fis.read())!=-1
用字元數組復制文件(fr 讀入流 ,fw寫出流),位元組流也是相同的用法
int i = 0; char[] c = new char[1024];
while((i = fr.reade()) !=-1)){
fw.write(c,0,i);
}
123456
3.對象流裡面套緩沖流的情景:
new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(「文件路徑」))));
4.記憶流及其功能的方法:
前綴表示功能,後綴表示流的類型;
比如說FileInputStream 前綴:File,表示操作的磁碟,後綴:intputstream,表示是位元組輸入流。
同理 FileReader:表示操作文件的字元流
ObjectInputStream :操作對象的位元組輸入流
5.拓展:獲取鍵盤輸入的字元的緩沖流的寫法:
new BufferedReader(new InputStreamReader(System.in)));
將位元組以字元形式輸出到控制台的字元緩沖流的寫法:
new BufferedWriter( new OutputStreamWriter(System.out))
Ⅳ java中位元組流轉成字元流再轉為位元組流保存成二進制文件會有什麼後果。
1. InputStream 和OutputStream,兩個是為字復節制流設計的,主要用來處理位元組或二進制對象,
2. Reader和 Writer.兩個是為字元流(一個字元占兩個位元組)設計的,主要用來處理字元或字元串.
字元流處理的單元為2個位元組的Unicode字元,分別操作字元、字元數組或字元串,而位元組流處理單元為1個位元組,操作位元組和位元組數組。所以字元流是由Java虛擬機將位元組轉化為2個位元組的Unicode字元為單位的字元而成的,所以它對多國語言支持性比較好!如果是音頻文件、圖片、歌曲,就用位元組流好點,如果是關繫到中文(文本)的,用字元流好點
所有文件的儲存是都是位元組(byte)的儲存,在磁碟上保留的並不是文件的字元而是先把字元編碼成位元組,再儲存這些位元組到磁碟。在讀取文件(特別是文本文件)時,也是一個位元組一個位元組地讀取以形成位元組序列
1,位元組流可用於任何類型的對象,包括二進制對象,而字元流只能處理字元或者字元串
Ⅵ 用java分別以位元組流和文本流方式實現文件的讀寫操作(先向test1.txt文件中寫「各位同學:
java創建TXT文件並進絕中肢行讀、寫、修改操作 import java.io.*;
/** *//**
*
* 功能描述:創建TXT文件並進行讀、寫、修改並世操作
*
* @author <a href="mailto:[email protected]">KenZhang</a>
* @version 1.0
* Creation date: 2007-12-18 - 下午06:48:45
*/
public class ReadWriteFile {
public static BufferedReader bufread;
//指定培或文件路徑和名稱
private static String path = "D:/suncity.txt";
private static File filename = new File(path);
private static String readStr ="";
/** *//**
* 創建文本文件.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已創建!");
}
}
/** *//**
* 讀取文本文件.
*
*/
public static String readTxtFile(){
String read;
FileReader fileread;
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "\r\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文件內容是:"+ "\r\n" + readStr);
return readStr;
}
/** *//**
* 寫文件.
*
*/
public static void writeTxtFile(String newStr) throws IOException{
//先讀取原有文件內容,然後進行寫入操作
String filein = newStr + "\r\n" + readStr + "\r\n";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
mm.writeBytes(filein);
} catch (IOException e1) {
// TODO 自動生成 catch 塊
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自動生成 catch 塊
e2.printStackTrace();
}
}
}
}
/** *//**
* 將文件中指定內容的第一行替換為其它內容.
*
* @param oldStr
* 查找內容
* @param replaceStr
* 替換內容
*/
public static void replaceTxtByStr(String oldStr,String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存該行前面的內容
for (int j = 1; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 將內容插入
buf = buf.append(replaceStr);
// 保存該行後面的內容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/** *//**
* main方法測試
* @param s
* @throws IOException
*/
public static void main(String[] s) throws IOException {
ReadWriteFile.creatTxtFile();
ReadWriteFile.readTxtFile();
ReadWriteFile.writeTxtFile("20080808:12:13");
// ReadWriteFile.replaceTxtByStr("ken", "zhang");
}
Ⅶ java分別以位元組流和字元流的兩種方式讀取文件內容
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
publicclassTest10{
/**
*復制當前的源程序到項目的根目錄
*@throwsIOException
*/
publicstaticvoidmain(String[]args)throwsIOException{
/*
*1:讀取原文件
*2:項目標文件中寫
*3:使用緩沖流按行讀寫
*/
FileInputStreamfis=newFileInputStream("src"+File.separator+"day01"+File.separator+"Test1.java");
//轉化為字元輸出流
InputStreamReaderisr=newInputStreamReader(fis);
//按行為單位讀取字元串
BufferedReaderbr=newBufferedReader(isr);
//
PrintWriterpw=newPrintWriter("Test1.java");
Stringline=null;
while((line=br.readLine())!=null){
pw.println(line);
}
pw.close();
br.close();
}
}
Ⅷ java 字元串流怎麼轉成文件
代碼如下:
/**
* 將一個字元串轉化為輸入版流權
*/
public static InputStream getStringStream(String sInputString){
if (sInputString != null && !sInputString.trim().equals("")){
try{
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
return tInputStringStream;
}catch (Exception ex){
ex.printStackTrace();
}
}
return null;
}
Ⅸ 關於java流的問題,位元組流與字元流,為什麼要用轉換流,把位元組轉換成字元流,為什麼不直接用字元流
你好,很高興為你解答。
正如你所說Java IO分兩種流 1)位元組流 InputStream OutputStream ;2)字元流 Reader Writer 。
為什麼需要用兩種流,而且要轉換,不直接用字元流。
簡單說來,就是字元流不能處理所有的情況,位元組流也不能處理所有的情況。所以需要兩種轉換。
先說一下二者的區別:
1)字元流處理的單元為2個位元組的Unicode字元,分別操作字元、字元賣肆數組或字元串,
2)而位元組流處理單元為1個位元組, 操作位元組和位元組數組。
位元組流可用於任何類型的對象,包括二進制對象,而字元流只能處理字元或者字元串; 位元組流提供了處理任何類型的IO操作的功能,但它不能直接處理Unicode字元,而友配鎮字元流就可以。
所以字元流是由Java虛擬機將位元組轉化為2個位元組的Unicode字元為單位的字元而成的,所以它對多好粗國語言支持性比較好!
如果是 音頻文件、圖片、歌曲,就用位元組流好點。
如果是關繫到中文(文本)的,用字元流好點。
Ⅹ JAVA:已知獲取文件的輸出流ouputStream,怎麼轉換為文件
Filefile1=newFile("a.txt");
Filefile2=newFile("b.txt");
FileChannelaChannel=newFileInputStream(file1).getChannel();
FileChannelbChannel=newFileOutputStream(file2).getChannel();
for(longcount=aChannel.size();count>0;){
longtransferred=aChannel.transferTo(aChannel.position(),count,bChannel);
aChannel.position(aChannel.position()+transferred);
count-=transferred;
}
通過NIO的方式將a文件拷貝到b文件,已知輸出流,就把輸出流轉通channel,通過開啟的channel通道來版相權互轉換,冰闊落遞一下。