-----------------------------------------------------
讀取字節:BufferedInputStream
讀取字符:BufferedReader + InputStreamReader
----------------------------------------------
import java.io.*;
--------------------------------------------------------------------------------
public class PrintStandardOutput {
String myAnswer = "No, and that's final,";
System.out.println("Hello World of Java");
System.out.println("The answer is " + myAnswer + " at this time.");
pw.println("The answer is " + myAnswer + " at this time.");
int i = 42;
pw.println(i + '=' + " the answer.");
pw.println("Note: " + i + '=' + " the answer.");
pw.println(i + "=" + " the answer.");
pw.println(i + ('=' + " the answer."));
}
}
-----------------------要讀取(輸出到—)一個文本文件-----------------------------
BufferedReader is=new BufferedReader(new FileReader("xxxx.text"));讀取
BufferedOutputStream byteout=new BufferedOutputStream(new FileOutputStream("XX.dat"));
// 寫出到文本!
-----------------------------------------------
at 04-09-20 10:49
aeonsun 網友說:
import java.io.*;
public class OpenFileByName {
public static void main(String[] args) throws IOException {
BufferedReader is = new BufferedReader(new FileReader("myFile.txt"));
BufferedOutputStream bytesOut = new BufferedOutputStream(
new FileOutputStream("bytes.dat"));
// Code here to read from is, write to bytesOut
bytesOut.close();
}
}
at 04-09-20 10:50
aeonsun 網友說:
import java.io.*;
public class FileIO {
private FileIO() { }
public static void copyFile(String inName, String outName)
throws FileNotFoundException, IOException {
BufferedInputStream is =
new BufferedInputStream(new FileInputStream(inName));
BufferedOutputStream os =
new BufferedOutputStream(new FileOutputStream(outName));
copyFile(is, os, true);
}
/** Copy a file from an opened InputStream to opened OutputStream */
public static void copyFile(InputStream is, OutputStream os, boolean close)
throws IOException {
int b; while ((b = is.read()) != -1) {
os.write(b);
}
is.close();
if (close)
os.close();
}
public static void copyFile(Reader is, Writer os, boolean close)
throws IOException {
int b; while ((b = is.read()) != -1) {
os.write(b);
}
is.close();
if (close)
os.close();
}
public static void copyFile(String inName, PrintWriter pw, boolean close)
throws FileNotFoundException, IOException {
BufferedReader is = new BufferedReader(new FileReader(inName));
copyFile(is, pw, close);
}
public static String readLine(String inName)
throws FileNotFoundException, IOException {
BufferedReader is = new BufferedReader(new FileReader(inName));
String line = null;
line = is.readLine();
is.close();
return line;
}
protected static final int BLKSIZ = 8192;
public void copyFileBuffered(String inName, String outName) throws
FileNotFoundException, IOException {
InputStream is = new FileInputStream(inName);
OutputStream os = new FileOutputStream(outName);
int count = 0;
byte b[] = new byte[BLKSIZ];
while ((count = is.read(b)) != -1) {
os.write(b, 0, count);
}
is.close();
os.close();
}
public static String readerToString(Reader is) throws IOException {
StringBuffer sb = new StringBuffer();
char[] b = new char[BLKSIZ];
int n;
while ((n = is.read(b)) > 0) {
sb.append(b, 0, n);
}
return sb.toString();
}
public static String inputStreamToString(InputStream is)
throws IOException {
return readerToString(new InputStreamReader(is));
}
public static void stringToFile(String text, String fileName)
throws IOException {
BufferedWriter os = new BufferedWriter(new FileWriter(fileName));
os.write(text);
os.flush();
os.close();
}
public static BufferedReader openFile(String fileName)
throws IOException {
return new BufferedReader(new FileReader(fileName));
}
}
at 04-09-20 10:50
aeonsun 網友說:
import java,io.*;
class fileprocess
{
public static void main(String args[])
{
int b;
byte buffer[]=new byte[1000];
try
{ b=System.out.read(buffer); //存取數據,以備寫出!
FileOutputStream out=new FileOotputStream("line.text");
out.write(buffer,0,b); //寫出!注意字節緩衝語文件流的關係
]
catch(IOException e)
{ System.out.println(" error");
}
]
}
at 04-09-20 10:50
aeonsun 網友說:
import java,io.*;
public class read
{
public static void main(String args[]0
{
int b;
byte buffer=new byte[2000];
try
{ FileInputStream readfile=new FileInputStream("XXXX.java");
b=readfile(buffer,0,25000);
try{String str=nre String(buffer,0,b,"Default");//構造String對象!
System.out.println(str);}
catch(UnsuportedEncodeingException e)
{ e.printStackTrace();}
catch( IOException e)
{ System.out.println(" error");}
}
}
at 04-09-20 10:51
aeonsun 網友說:
-----------------------------------------------------------------------
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
---------------利用 BufferedReader--FileReader讀取文本文件!-----------
---------------在IO中始終要注意是字節流還是字符流----------------------
class filewindow extends JFrame implements ActionListener
{
JTextArea text;
BufferedReader in;
JButton button;
FileReader file;
filewindow()
{
super("文件字符流");
Container con=getContentPane();
text=new JTextArea(50,50);
text.setBackground(Color.blue);
try{
File f=new File("E://a.txt");
file=new FileReader(f);
in=new BufferedReader(file);
/**BufferedReader(Reader in)構造函數,
*文件自字符讀取流FileReader接入BufferedReader
*流中,以便用BufferedReader的對象方法readLine()高效成行讀取!
*/
}
catch(FileNotFoundException e){}
catch(IOException e){}
button=new JButton("讀取");
button.addActionListener(this);
con.setLayout(new BorderLayout());
setSize(300,200);
setVisible(true);
con.add(text,"Center");
con.add(button,"South");
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{
String s;
if(e.getSource()==button)
try{
while((s=in.readLine())!=null)
text.append(s+'/n');
//在這裏大家還可以用BufferString來暫時保存讀取的字符數據!
}
catch(IOException e1){}
}
//---------main()----------
public static void main(String args[])
{
filewindow win=new filewindow();
win.pack();
}
}
at 04-09-20 10:51
aeonsun 網友說:
-------------------RandomAccessFile隨機讀取文件---------------
import java.io.*;
public class RandomRead
{
final static String FILENAME="E://a.txt";
protected String fileName;
protected RandomAccessFile seeker;
public static void main(String[] argv) throws IOException {
RandomRead r = new RandomRead(FILENAME);
System.out.println("Offset is " + r.readOffset());
System.out.println("Message is /"" + r.readMessage() + "/".");
}
/** Constructor: save filename, construct RandomAccessFile */
public RandomRead(String fname) throws IOException {
fileName = fname;
seeker = new RandomAccessFile(fname, "rw");
}
/** Read the Offset field, defined to be at location 0 in the file. */
public int readOffset() throws IOException {
seeker.seek(0);
seeker.writeChars(FILENAME); // move to very beginning
return seeker.readInt(); // and read the offset
}
/** Read the message at the given offset */
public String readMessage() throws IOException {
seeker.seek(120); // move to where
return seeker.readLine(); // and read the String
}
}
at 04-09-20 10:51
aeonsun 網友說:
一些評論:axman
寫得很辛苦,我本來不想再說什麼了,但本着對技術負責的精神還是說出來.
對於I/O的理解屬於3級水平(如果java IO有十級的話)
錯誤太多.
對於I/O層次不熟悉
java IO主要包括
java.io包和java.nio包.
java.io主要從四個接口延伸:
字節:
InputStream/OutputStream,其下爲封裝,過濾,特定對象處理的具體實現類.
字符:
Reader/Writer(原文中連Writer接口全都寫成Write,足以說明根本不瞭解這了接口,如果你經常使用Writer接口怎麼會連Writer和Write都分不清,不是一處失誤,而是全部都是Write)
以上四個接口中,底層全部是操作字節的阻塞方式流.
java.io主要以塊操作塊(Buffer)爲主,通過可以設定的阻塞和非陰塞模式,極大地提過了數據輸出輸
入的性能,而且將Channel通過選選擇器模式的控制,可以實現在同一輸出輸入通道上多用戶併發進行數據傳輸入.比如一個Socket端口可以同時被無限多(理論上不受限制)個客戶端併發訪問.就是經典的I/O多路複用技術.
{
public static void main(String args[])
{ try{ //流轉換!
BufferedReader is=new BufferedReader(new InputStreamReader(System.in))
String inputline=null;
while((inputline=is.readLine())!=null)
System.out.println(inputline);
is.close();
}
catch(IOException e)
{ System,out.println("IOXE: "+e);
}
}
}