java io操作代码

----------标准设备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);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值