JAVA的IO操作之二:執行個體及序列化

來源:互聯網
上載者:User

一,常見讀寫樣本:
(1).讀寫文字檔,程式功能:讀取一文字檔到記憶體中的String類型變數中,在終端顯示內容,並複製內容到另外的文字檔中。
package MyJava.Base;
import java.io.*;
public class TextReaderDemo
{
     public static void main(String[] args) throws IOException
    {
         //讀取檔案中內容到BufferedReader;
        BufferedReader in=new BufferedReader(new FileReader("E://Java//JCreator2.5//加密解密.txt"));
        String str=new String();
        String s=new String();
        while((s=in.readLine())!=null)
               str+=s+"/n";
        in.close();
        System.out.println(str);
  
        PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("E://Java//JCreator2.5//加密解密(copyt).txt")));
        BufferedReader outbfreader=new BufferedReader(new StringReader(str));
        while((s=outbfreader.readLine() )!=null)
              out.println(s);
        out.close();
}
(2).從控制台輸入,Java中從控制台輸入比起C++好像複雜多了,畢竟C++只需要cin和cout就行了,Java有點複雜
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String str=stdin.readLine();
 
(3).從記憶體中讀取
StringReader in2=new StringReader(str); //其中str為String類型
int ch;
while((ch=in2.read())!=-1)
     System.out.printl((char)ch);

二,Java的序列化
     

 

參考原文:http://blog.csdn.net/Cpp_Java_Man/archive/2006/05/02/705271.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.