java io作業碼

來源:互聯網
上載者:User
----------標準裝置System.in讀取資料------------------
-----------------------------------------------------
讀取位元組:BufferedInputStream
讀取字元:BufferedReader + InputStreamReader
----------------------------------------------
import java.io.*;


--------------------------------------------------------------------------------
-----------------標準輸出System.out是一個列印流PrintStream------------------

import java.io.*;public class PrintStandardOutput {public static void main(String[] args) {
String myAnswer = "No, and that's final,";
System.out.println("Hello World of Java");
System.out.println("The answer is " + myAnswer + " at this time."); PrintWriter pw = new PrintWriter(System.out);
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.")); pw.close();
}
}

-----------------------------------------------------------------------------------
-----------------------要讀取(輸出到—)一個文字檔-----------------------------

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 class systemin
{
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);
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.