分类: Java
2008-08-03 00:08:21
一、执行基本的输入输出:
//file:IOApp.java
package test;
import java.io.*;
public class IOApp{
public static void main(String args[]){
byte buffer[] = new byte[255];
System.out.println("\nTye a line of text:");
try{
System.in.read(buffer,0,255);
}catch(Exception e){
String err = e.toString();
System.out.println(err);
}
System.out.println("\nThe line you typed was:");
try{
String inputStr = new String(buffer,"Default");
System.out.println(inputStr);
}catch(UnsupportedEncodingException e){
System.out.println("e:" + e);
}
}
}
输出:
System.out.println("String");
输入:
System.in.read(buffer,0,255);
获取输入的字符串:
String inputStr = new String(buffer,"Default");
二、读取文本文件中的内容
//file:FileApp.java
package test;
import java.io.*;
public class FileApp{
public static void main(String args[]){
byte buffer[] = new byte[2056];
try{
FileInputStream fileIn = new FileInputStream("untitled-1.png");
int bytes = fileIn.read(buffer,0,2056);
try{
String str = new String(buffer,0,bytes,"Default");
System.out.println(str);
}catch(UnsupportedEncodingException e){
System.out.println("The encoding \"Default\" was not found:" + e);
}
}
catch(Exception e){
String err = e.toString();
System.out.println(err);
}
}
}
将数据读入缓冲:
FileInputStream fileIn = new FileInputStream("filename");
int bytes = fileIn.read(buffer,0,2056);
从缓冲中读出读入的数据:
String str = new String(buffer,0,bytes,"Default");
三、从输入设备读入一行文本并存入文件
//file:FileApp2.java
package test;
import java.io.*;
public class FileApp2{
public static void main(String args[]){
byte buffer[] = new byte[80];
try{
System.out.println("\nEnter a line to be saved to disk:");
int bytes = System.in.read(buffer);
FileOutputStream fileOut = new FileOutputStream("line.txt");
fileOut.write(buffer,0,bytes);
}catch(Exception e){
String err = e.toString();
System.out.println(err);
}
}
}
输入:
int bytes = System.in.read(buffer);
将输入的文本存入文件:
FileOutputStream fileOut = new FileOutputStream("line.txt");
fileOut.write(buffer,0,bytes);
四、列出所有以.java结尾文件
//file:ListJava.java
package test;
import java.io.*;
public class ListJava extends Object{
public static void main(String args[]){
//Create a File instance for the current directory
File currDir = new File("test");
//Get a filtered list of the .java files in the current directory
String[] javaFiles = currDir.list(new JavaFilter());
//Print out the contents of the javaFiles array
for(int i=0;iSystem.out.println(javaFiles[i]);
}
}
}
JavaFilter.java文件
//file:JavaFilter.java
package test;
import java.io.*;
//This class implements a filename filter that only allows
//file that ends with .java
public class JavaFilter extends Object implements FilenameFilter{
public JavaFilter(){}
public boolean accept(File dir,String name)
{
//Only return true for accept if the file ends with .java
return name.endsWith(".java");
}
}
获取目录:
File currDir = new File("test");
将文件列表读入数组:
String[] javaFiles = currDir.list(new JavaFilter());
输出文件列表:
for(int i=0;iSystem.out.println(javaFiles[i]);
}
设定要列表文件的扩展名:
在JavaFilter类的accept方法中:
return endsWith(".java");
五、创建临时文件,并在退出进删除
//file:TestFile.java
package test;
import java.io.*;
public class TestFile {
public static void main(String args[]) {
try {
File tempFile = File.createTempFile("test#imagebear", ".tmp");
FileOutputStream fout = new FileOutputStream(tempFile);
PrintStream out = new PrintStream(fout);
out.println("Place this test string in the temp file");
tempFile.deleteOnExit();
} catch (IOException ioe) {
System.out.println(
"There was a problem creating/writing to the temp file");
ioe.printStackTrace(System.err);
}
System.out.println(
"Until you hit 'enter' there is a temp file on the system");
try {
System.in.read();
} catch (IOException e) {
}
System.exit(0);
}
}
创建临时文件:
File tempFile = File.createTempFile("test#imagebear",".tmp");
FileOutputStream fout = new FileOutputStream(tempFile);
PrintStream out = new PrintStream(fout);
退出时删除:
tempFile.deleteOnExit();
退出:
System.exit(0);
六、使用RandomAccessFile读取和显示文本文件内容
//file:FileApp3.java
package test;
import java.io.*;
public class FileApp3 {
public static void main(String args[]) {
try {
RandomAccessFile file = new RandomAccessFile("test/FileApp3.java", "r");
long filePointer = 0;
long length = file.length();
while (filePointer < length) {
String s = file.readLine();
System.out.println(s);
filePointer = file.getFilePointer();
}
}catch(Exception e){
String err = e.toString();
System.out.println(err);
}
}
}
构造RandomAccessFile对象:
RandomAccessFile File = new RandomAccessFile("test/FileApp3.java","r");
初始化文件指针:
long filePointer = 0;
获取文件的长度:
long length = File.length();
读取并显示文件的内容:
while(filePointer < length){
String s = File.readLine();
System.out.println(s);
filePointer = File.getFilePointer();
}
import java.io.*;
public class CharInput
{
public static void main(String args[])
{
String s;
try
{
FileInputStream is=new FileInputStream("suoxin.txt"); //读出指定的文件,二进制流
InputStreamReader ir=new InputStreamReader(is); //读取二进制格式,字符流
BufferedReader in =new BufferedReader(ir); //读取字符流,变成反冲字符流
while((s=in.readLine()) !=null) //以行读取
System.out.println("Read:" +s);
in.close();
}
catch(IOException e)
{
}
}
}
使用RandomAccessFile从输入设备读入一行文本并存入文件
import java.io.*;
public class RandomFileRW
{
public static void main(String args[])
{
StringBuffer buf=new StringBuffer();
char ch;
try
{
while( (ch=(char)System.in.read()) !='\n')
{
buf.append( ch);
} //读写方式可以为"r" or "rw"
RandomAccessFile myFileStream=new RandomAccessFile("Input.txt","rw");
myFileStream . seek(myFileStream.length()) ; //定为指针到文件尾
myFileStream.writeBytes(buf.toString()); //将用户从键盘输入的内容添加到文件的尾部
myFileStream.close();
}
catch(IOException e)
{
}
}
}
关于IO流的一些具体介绍 请看一下连接内容