给你一个拷贝文件的例子吧!!/*
* Created on 2004-9-11
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package test;import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* @author 崔占民
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class copy
{
final private static int INT_READLEN = 10240; public static void main(String[] args)
{
if (args.length == 0 || (args.length == 1 && !args[0].equals("/?")))
{
System.out.println("command syntax is invalidity!");
return;
}
else if (args[0].equals("/?"))
{
System.out.println("copy SrcFile DesFile");
System.out.println("SrcFile: Source File.");
System.out.println("DesFile: destination File.");
return;
}
copy cpy = new copy(args[0], args[1]);
} copy(String aStrSrcFile, String aStrDesFile)
{
if (aStrSrcFile.equals("") || aStrDesFile.equals(""))
{
System.out.println("please input SrcFile and DesFile!");
System.out.println("copy SrcFile DesFile");
return;
} FileInputStream in = null;
FileOutputStream out = null;
try
{
int bytes = 0;
byte[] bteFile = new byte[INT_READLEN];
in = new FileInputStream(aStrSrcFile);
out = new FileOutputStream(aStrDesFile); while ((bytes = in.read(bteFile)) != -1)
{
out.write(bteFile, 0, bytes);
}
System.out.println("File Copy finished!");
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
in.close();
out.close();
}
catch (Exception e)
{
System.out.println(e.toString());
return;
}
}
}
}
* Created on 2004-9-11
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package test;import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* @author 崔占民
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class copy
{
final private static int INT_READLEN = 10240; public static void main(String[] args)
{
if (args.length == 0 || (args.length == 1 && !args[0].equals("/?")))
{
System.out.println("command syntax is invalidity!");
return;
}
else if (args[0].equals("/?"))
{
System.out.println("copy SrcFile DesFile");
System.out.println("SrcFile: Source File.");
System.out.println("DesFile: destination File.");
return;
}
copy cpy = new copy(args[0], args[1]);
} copy(String aStrSrcFile, String aStrDesFile)
{
if (aStrSrcFile.equals("") || aStrDesFile.equals(""))
{
System.out.println("please input SrcFile and DesFile!");
System.out.println("copy SrcFile DesFile");
return;
} FileInputStream in = null;
FileOutputStream out = null;
try
{
int bytes = 0;
byte[] bteFile = new byte[INT_READLEN];
in = new FileInputStream(aStrSrcFile);
out = new FileOutputStream(aStrDesFile); while ((bytes = in.read(bteFile)) != -1)
{
out.write(bteFile, 0, bytes);
}
System.out.println("File Copy finished!");
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
in.close();
out.close();
}
catch (Exception e)
{
System.out.println(e.toString());
return;
}
}
}
}
那怎么来理解呢?既然是in/out那肯定是相对于一个设备或者一个对象的,比如console的标准输入输出(standard in/out),那么console上显示的就靠out写出来的,你的输入就是in读进去。
^^^^^^^^^ ^^^^^^^^理解了这个,再去理解read/writer就方便了。
System.in()输入的InputStream,它是一切字节流的父类,因此可以回答你说System.in输入的是任何字节流。
当然你在具体使用的时候可以转化为具体的子类流,甚至通过InputStreamReader转化为字符流进行显示。BufferedInputStream/BufferedOutputStream分别是FilterInputStream/FilterOutputStream的子类
Filter流类的作用是在源和目标之间建立一个中间流,可以对流进行删除、修改、添加、合并数据等等操作。
BufferedInputStream/BufferedOutputStream则是Filter中间流的一种,用来将数据读写到内存中(可理解为
读写到一个可指定size的byte数组中),这样后续操作就不用老是从硬盘上读文件了,直接在内存中即可。BufferedReader/BufferedWeiter也是用样的原理为更高效的读写字符流InputStreamReader/OutputStreamWriter是字节流向字符流转化的工具,但必须明确字符流是经过编码后的字节流
它本质还是字节,故不存在字符流向转化字节流的问题。另加一句,sun已经用java.nio.*重写了一遍java.io.*中的实现,因此一般的应用直接使用java.io就可以。
除非涉及的一些更深入的编程,这时才需要使用java.nio包
我觉得很多人迷信think in java是不对的,它其实还是没有很深入的涉及到java的实现思想。
而且java给人一种入门很容易的感觉,殊不知其内功练起来格外累人。
建议看看一些专项的经典教程。Java I/O ,java threads ,Java.Network.Programming,Oreilly出版的
好像也不是很清楚。
其实, i/o无非两件事 in 和out
然后 in 有哪些方式, 也不外就是 file ,keyboard ,( net属于file), String 三个,
不知,think in java将它搞哪个复杂干什么,建议到 www.infoxa.com 下本
java2 ..pri...什么的看看, I/O你才会彻底明白。
launch401(宁静温泉),说得对就是一个decorator模式,但是对于初学者你不能让他先学decorator模式再学java i/o吧:)
如果你看过第三版的thinking in java(暂无中文版)你会知道,作者自己已经将i/o这一章重写了
java i/o比较乱主要是历史原因造成的,有两大体系,再加上decorator,用到的类比较多,初学者应该把两大体系分清楚,再搞明白decorator,就差不多了。
类库这种东西,其实完全看java doc就可以搞定,但最好还是明白他的设计结构,可惜目前好像没有这方面的书,只能看源代码。
强烈支持,这个观点。这也是java学习瓶颈之一
我们好像争论的话题和楼主的问题不相关也
交个朋友如何,我的msn:[email protected],我是学习ing
你首先要In过来
然后要Out出去然后你可以读一个图片,并把它另存到一个目录,你看到有一个图片保存到的你指定的路径
其实最重要的是理解他们的继承关系,然后根据实际情况做一下练习!这样比较快的!
最后你发现流虽然很重要,但是对Java整体的把握才是最重要的!
这也体现OOP的精髓!
记得刚学java的时候,对流的理解很是让人恼怒,后来明白了流的概念之后,对于一个流的构造方法里面在new一个流感到莫名其妙.现在终于有点眉目,故此记录如下:
1.流的理解,
输入流--你可以把它看作是来源,即所要东西的源头.比如你想要得到一个文件里面的咚咚,那么初始化成输入流即可.
输出流--你可以把它看作是写字板(打个比方),比如你想在一个文件里面写入东西,那么初始化成输出流 2.流的嵌套使用--
先看看流的分类(我自己分的类命的名,^_^).a.基础流b.包装流.流的嵌套是这样用的 new 包装流(new 基础流).
ok,明白了没?最外层的是包装流,里面的是基础流(人喜欢打扮,流也不例外).当然,包装会有包装的好处,比如包装后的流可以调用包装流的方法,满足不同人的不同要求.
基础流:InputStream,ByteArrayInputStream,FileInputStream等等(包括对应的OutputStream)
包装流:FilterInputStream,BufferedInputStream,DataInputStream, LineNumberInputStream,PushbackInputStream以及有关的OutputStream.
现在认为一个字节是位的一个字符比如一个int 是四个字节
计算机输出的基本单位是字节用流输出也是一个字节一个字节的,
而字符流里有一个一行一行输出的方法所以经常把字节流转换成字节流这中间用管道流套接
是不是这样的啊!
可以分为输入/输出流,还可以分为字节流和字符流,还可以分为基础流和过滤流,可以根据自己的需要使用。一般的应用使用过滤流(实际上是功能增强的流)的时候比较多,比如现在想通过缓冲(即一种过滤流)字符流读一个文件的话,可以这么写: BufferedReader br = new BufferedReader ( //转换为缓冲文件字符流
new InputStreamReader( //转换为文件字符流
new FileInputStream("c:\\a.txt")));//文件字节流
专题:设计模式在JAVA IO库中的应用
看完了你就明白了
奇怪的是 我以前看明白了,现在又糊涂了,一会再看下。
对于字节流,不同的存储方式:bytearray,file,stringbuffer,pipe,特殊处理:buffered,filter
buffered和filter是用来配合“负责不同数据存储方式的类”使用的!
其中最常用的是buffered
可以序列化你这个类我这里有写了一个比较模块化适合你看。其实java流对象分成了2种,in和out然后in和out又因为对付不同的流对象分成了4种不同的,就像前面贴子人的,你多看看类的说明,用自己适合的流类/*
* Created on 2005-3-15
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package IOTest;import javax.swing.JFrame;
import java.io.*;
import javax.swing.*;import java.awt.BorderLayout;
import java.awt.event.*;/**
* @author AppleTian
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class accountFile extends JFrame {
//private userAccount myAccount;
//private
private ObjectOutputStream output; private ObjectInputStream input; private UserInterface myface; private JButton openButton, enterButton, openAObject;
private static int kkk=0; public accountFile() {
super("create my account...");
myface = new UserInterface(2);
getContentPane().add(myface, BorderLayout.CENTER);
openButton = myface.getDoTask1();
openButton.setText("save into file...");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
enterButton = myface.getDoTask2();
enterButton.setText("enter");
enterButton.setEnabled(false);
enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addRecord();
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (output == null) {
addRecord();
}
closeFile(); }
});
openAObject = myface.getDoTask3();
openAObject.setText("openMyFile..");
openAObject.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(kkk==0){
openAnAccount();
kkk++;
}
disPlayAnAccount();
}
});
setSize(300, 200);
setVisible(true); } /**
*
*/
protected void disPlayAnAccount() {
try{
userAccount aAccount=(userAccount) input.readObject();
String values[]={String.valueOf(aAccount.getAccountNumber()),String.valueOf(aAccount.getAccountName())};
myface.setFieldsValues(values);
}
catch(IOException e){
JOptionPane.showMessageDialog(this,"IOExceptionInDisplay","IOException",JOptionPane.ERROR_MESSAGE);
}
catch(ClassNotFoundException ee){
JOptionPane.showMessageDialog(this,"IOException","IOException",JOptionPane.ERROR_MESSAGE); }
}
/**
*
*/
protected void openAnAccount() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fileChooser.showSaveDialog(this); if (result == JFileChooser.CANCEL_OPTION)
return; File fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals("")) {
JOptionPane.showMessageDialog(this, "Invalid file name",
"Invalid filename", JOptionPane.ERROR_MESSAGE);
}
try {
input = new ObjectInputStream(new FileInputStream(fileName)); } catch (IOException e) {
JOptionPane.showMessageDialog(this, "Erro open file", "Error",
JOptionPane.ERROR_MESSAGE); } } /**
*
*/
protected void addRecord() {
userAccount account;
String[] values = myface.getFieldsValues();
try {
int num = Integer.parseInt(values[0]);
if (num > 0) {
account = new userAccount(num, values[1]);
output.writeObject(account);
output.flush(); }
myface.clearFields();
} catch (NumberFormatException ee) {
closeFile();
} catch (IOException e) {
//JOptionPane.showMessageDialog()
closeFile();
}
catch(NullPointerException eee){
closeFile();
} } /**
*
*/
private void closeFile() {
try {
if (output != null) { output.close();
}
if(input!=null){
input.close();
}
System.exit(0);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "close Error",
"Error occured in closing the file",
JOptionPane.ERROR_MESSAGE);
} } /**
*
*/
protected void openFile() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fileChooser.showSaveDialog(this); if (result == JFileChooser.CANCEL_OPTION)
return; File fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals("")) {
JOptionPane.showMessageDialog(this, "Invalid file name",
"Invalid filename", JOptionPane.ERROR_MESSAGE);
}
try {
output = new ObjectOutputStream(new FileOutputStream(fileName));
openButton.setEnabled(false);
enterButton.setEnabled(true);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Erro open file", "Error",
JOptionPane.ERROR_MESSAGE); } }
public static void main(String[] args) {
new accountFile(); }
}这个是主类 /*
* Created on 2005-3-15
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package IOTest;import java.io.Serializable;/**
* @author AppleTian
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class userAccount implements Serializable {
private int accountNum;
private String accountName;
public userAccount(){
this(0,"");
}
public userAccount(int num,String name){
accountNum=num;
accountName=name;
}
public int getAccountNumber(){
return accountNum;
}
public String getAccountName(){
return accountName;
}
public void setAccountNumer(int num){
accountNum=num;
}
public void setAccountName(String name){
accountName=name;
}}
这个是写的类的对象要存储的对象
/*
* Created on 2005-3-15
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package IOTest;import java.awt.BorderLayout;
import java.awt.GridLayout;import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;/**
* @author AppleTian
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class UserInterface extends JPanel {
protected static String[] names={"acountNumber","name"};
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1,doTask2,doTask3;
protected JPanel innerPanelCenter,innerPanelSouth;
protected int size;
public UserInterface(int mysize){
size=mysize;
labels=new JLabel[size];
fields=new JTextField[size];
for(int count=0;count<labels.length;count++){
labels[count]=new JLabel(names[count]);
}
for(int count=0;count<fields.length;count++){
fields[count]=new JTextField("");
}
innerPanelCenter =new JPanel();
innerPanelCenter.setLayout(new GridLayout(size,2));
for(int k=0;k<size;k++){
innerPanelCenter.add(labels[k]);
innerPanelCenter.add(fields[k]);
}
doTask1=new JButton();
doTask2=new JButton();
doTask3=new JButton();
innerPanelSouth=new JPanel();
innerPanelSouth.add(doTask1);
innerPanelSouth.add(doTask2);
innerPanelSouth.add(doTask3);
setLayout(new BorderLayout());
add(innerPanelCenter,BorderLayout.CENTER);
add(innerPanelSouth,BorderLayout.SOUTH);
validate();
}
public JButton getDoTask1(){
return doTask1;
}
public JButton getDoTask2(){
return doTask2;
}
public JButton getDoTask3(){
return doTask3;
}
public JTextField[] getFields(){
return fields;
}
public void clearFields(){
for(int k=0;k<size;k++){
fields[k].setText("");
}
}
public void setFieldsValues(String[] values) throws IllegalArgumentException {
if(values.length!=size)
throw new IllegalArgumentException("There must be"+size+"strings in it");
for(int k=0;k<size;k++){
fields[k].setText(values[k]);
}
}
public String[] getFieldsValues(){
String[] values=new String[size];
for(int k=0;k<size;k++){
values[k]=fields[k].getText();
}
return values;
}}
这个是界面类你可以自己看看代码