您的位置:首页 > 编程语言 > Java开发

Java IO实战操作(二)

2012-04-29 22:29 417 查看
/**
* 向文件中追加新内容
* @throws  IOException
*/
public void NewInserNum() throws IOException {
String fileName = "D:" + File.separator + "IO.txt";
File file = new File(fileName);
OutputStream out = new FileOutputStream(file, true);
String str = "我在成都名流学习SoftWare";
byte[] Bbyte = str.getBytes();
for (int i = 0; i < Bbyte.length; i++) {
out.write(Bbyte[i]);
}
out.close();
}

/**
* 获取准确大小空间读取文件
* @throws IOException
*/
public void SelectFile() throws IOException {
String fileName = "D:" + File.separator + "IO.txt";
File file = new File(fileName);
InputStream in = new FileInputStream(file);
byte[] Bbyte = new byte[(int) file.length()];
in.read(Bbyte);
System.out.println("文件长度为:" + file.length());
in.close();
System.out.println(new String(Bbyte));
}

/**
* 一个一个的读
*/
public void OneSelectFile() throws IOException {
String fileName = "D:" + File.separator + "hello.txt";
File f = new File(fileName);
InputStream in = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) in.read();
}
in.close();
System.out.println(new String(b));
}

/**
* 不知道文件有多大的情况下读取文件
*/
public void NotSelectFile() throws IOException{
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[1024];
int count =0;
int temp=0;
while((temp=in.read())!=(-1)){
b[count++]=(byte)temp;
}
in.close();
System.out.println(new String(b));
}

/**
* 字符流
* @throws IOException
*/
public void StringFile() throws IOException{
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
Writer out =new FileWriter(f);
String str="hello";
out.write(str);
out.close();
}
/**
* 字符流
* 从文件中读出内容
*/
public void OutStringFile() throws IOException{
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
char[] ch=new char[100];
Reader read=new FileReader(f);
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为"+new String(ch,0,count));
}

/**
* 文件复制
* @throws IOException
*/
public void CoutStringFile(String[]args) throws IOException{
if(args.length!=2){
System.out.println("命令行参数输入有误,请检查");
System.exit(1);
}
File file1=new File(args[0]);
File file2=new File(args[1]);
if(!file1.exists()){
System.out.println("被复制的文件不存在");
System.exit(1);
}
InputStream input=new FileInputStream(file1);
OutputStream output=new FileOutputStream(file2);
if((input!=null)&&(output!=null)){
int temp=0;
while((temp=input.read())!=(-1)){
output.write(temp);
}
}
input.close();
output.close();
}
/**
* 将字节输出流转化为字符输出流
* @throws IOException
*/
public void OutputStream() throws IOException{
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Writer out=new OutputStreamWriter(new FileOutputStream(file));
out.write("hello");
out.close();
}
/**
* 将字节输入流变为字符输入流
* @throws IOException
*/
public void InputStream() throws IOException{
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Reader read=new InputStreamReader(new FileInputStream(file));
char[] b=new char[100];
int len=read.read(b);
System.out.println(new String(b,0,len));
read.close();
}
/**
* 使用内存操作流将一个大写字母转化为小写字母
* @throws IOException
*/
public void ByteArray() throws IOException{
String str="ROLLENHOLT";
ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream output=new ByteArrayOutputStream();
int temp=0;
while((temp=input.read())!=-1){
char ch=(char)temp;
output.write(Character.toLowerCase(ch));
}
String outStr=output.toString();
input.close();
output.close();
System.out.println(outStr);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: