Java IO Test

package com.xiaoge;
import java.io.*;


public class Test1 {
 
public static void main(String args[]){
FileInputStream in =  null;
int b = 0;

try{
in = new FileInputStream("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test1.java");

}catch(FileNotFoundException ex){
System.out.println("找不到指定文件");
System.exit(-1);
}
try{
long num = 0;
while((b = in.read()) != -1){
System.out.print((char)b);
num++;
}
in.close();
System.out.println("一共读取了"+num+"个字节");
}catch(IOException ex){
System.out.println("文件读取错误");
System.exit(-1);
}
}

}



package com.xiaoge;
import java.io.*;


public class Test3 {
public static void main(String args []){
FileReader fr = null;
int b = 0;


try{
long num = 0;
fr = new FileReader("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test3.java");
while((b = fr.read()) != -1){
System.out.print((char)b);
num++;
}
System.out.println("一共"+num+"字符");

}catch(FileNotFoundException e){

}catch(IOException e){

}

}

}


package com.xiaoge;
import java.io.*;


public class Test4 {

public static void main(String args[]){
FileWriter fw = null;

try{
fw = new FileWriter("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test04.dat");
for(int c = 0; c < 50000; c++){
fw.write(c);

}
fw.close();

}catch(IOException e){
e.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);

}


}


}



package com.xiaoge;
import java.io.*;


public class Test5 {
public static void main (String args[]){

FileInputStream fis = null;
BufferedInputStream bis = null;

try{
fis = new FileInputStream("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test5.java");
bis = new BufferedInputStream(fis);
int c = 0;
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.mark(100);
for(int i = 0; i < 10 && (c = bis.read()) != -1; i++){
System.out.print((char)c+" ");
}
System.out.println();
bis.reset();
for(int i = 0; i < 10 && (c = bis.read()) != -1; i++){
System.out.print((char)c+" ");
}
System.out.println();
bis.close();
}catch(FileNotFoundException e){
}catch(IOException e){
}
}
}



package com.xiaoge;
import java.io.*;


public class Test6 {

public static void main(String args[]){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test6.txt"));
BufferedReader br = new BufferedReader(new FileReader("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test6.java"));
int c;
String s = null;

while((c = br.read()) != -1){
bw.write(c);
}
bw.flush();
while((s = br.readLine()) != null){
System.out.println(s);
}
br.close();
bw.close();

}catch(FileNotFoundException e){

}catch(IOException e){

}
}
}



package com.xiaoge;
import java.io.*;


public class Test7 {
public static void main(String args []){
try{
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(
"D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test7.txt"));
osw.write("hahfahkjghkjdaj");
System.out.println(osw.getEncoding());
osw.close();

osw = new OutputStreamWriter(
new FileOutputStream(
"D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test7.txt", true),"ISO8859_1");
osw.write("hhhhjhjhjhkj");
System.out.println(osw.getEncoding());
osw.close();
}catch(IOException e){

}

}


}



package com.xiaoge;
import java.io.*;
public class Test8 {
public static void main(String args[]){
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = null;
try{
s = br.readLine();
while(s != null){
if(s.equalsIgnoreCase("exit")){
break;
}
System.out.println(s.toUpperCase());
s = br.readLine();
}
}catch(IOException e){
e.printStackTrace();
}
}
}



package com.xiaoge;
import java.io.*;


public class Test9 {
public static void main(String args[]){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try{
dos.writeDouble(Math.random());
dos.writeBoolean(true);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
System.out.println(bais.available());
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
dos.close();
dis.close();
}catch(IOException e){
e.printStackTrace();
}
}


}



package com.xiaoge;
import java.io.*;


public class Test10 {
public static void main(String args[]){
FileOutputStream fos =null;// new FileOutputStream("");
PrintStream ps = null;
try{
fos = new FileOutputStream("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test010.dat");
ps = new PrintStream(fos);
}catch(IOException e){
e.printStackTrace();
}
if(ps != null){
System.setOut(ps);

}
int ln = 0;
for(char c = 0; c < 60000; c++){
System.out.print(c+" ");
if(ln++ > 0){
System.out.println();
ln = 0;
}
}
}
}



package com.xiaoge;
import java.io.*;


public class Test11 {
public static void main(String args[]){
String filename = args[0];
if(filename != null){
list(filename, System.out);
}
}

public static void list(String filename, PrintStream ps){
try{
BufferedReader br = new BufferedReader(new FileReader(filename));
String s = null;
while((s = br.readLine()) != null){
ps.println(s);
}
br.close();
}catch(IOException e){
e.printStackTrace();
ps.println("无法读取文件");
}
}
}


package com.xiaoge;


import java.util.*;
import java.io.*;


public class Test12 {
public static void main(String args[]){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
FileWriter fw = new FileWriter("D:\\workspace\\IOTest\\src\\com\\xiaoge\\Test012.log",true);
PrintWriter log = new PrintWriter(fw);
String s = null;
/*while((s = br.readLine()) != null){
if(s.equalsIgnoreCase("exit")){
break;
}
System.out.println(s.toUpperCase());;
log.println("--------------------");
log.println(s.toUpperCase());
log.flush();
}*/
log.println("---"+new Date()+"---");
log.flush();
log.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值