java基础18-io

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.Buffer;
import java.text.DateFormat;
import java.util.Date;




public class OutputDemo {


/**
* @param args
* @throws IOException 
* @throws Exception 
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
/*FileOutputStream fos = new FileOutputStream("E://JAVA代码/pack/B.txt");
byte[] buf="abcde/r/nasgbbdgbdbgddsn\r\nndghgh".getBytes();
fos.write(buf);

fos.close();
*/
/* FileOutputDemo fod=new FileOutputDemo();
fod.out();*/
/*FileInputDemo fid=new FileInputDemo();
fid.in();*/
/*CopyPicture cp=new CopyPicture();
cp.copy();*/
/*OtherCopy oc=new OtherCopy();
oc.systemDemo();*/
/*TransStream ts=new TransStream();
ts.Trans();*/
/*TransDemo2 ts=new TransDemo2();
ts.Trans();*/
FileListDemo fld=new FileListDemo();
fld.method();


         }


}


class FileListDemo{
public void method(){//获取文件列表
File[] roots=File.listRoots();//获取系统跟
for(File root:roots){
System.out.println(root);
}
}
}


class FileDemo{

public void method6(){
File f=new File("a.txt");
File f1=new File("c://b.txt");
f.renameTo(f1);//将f的文件名改为f1的文件名。//剪切重命名。
}
public void method5(){//获取
File f=new File("abc");
long time=f.lastModified();//最后一次修改的时间。
Date d=new Date(time);
DateFormat df=DateFormat.getDateInstance(DateFormat.LONG);
String s=df.format(df);
System.out.println(s);
f.length();//获取文件的长度。返回文件的字节数。只针对于文件而言。
f.getAbsoluteFile();
f.getAbsolutePath();//获取的封装的绝对路径
f.getPath();//获取file内封装的路径内容
f.getName();
f.getParent();//获取父目录
}
public void method4(){//判断是否是可执行程序
File f=new File("abc");
Runtime r=Runtime.getRuntime();
if(f.canExecute()){//判断是否可执行
try {
r.exec("abc");//执行可执行程序
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void method3(){//判断
File f=new File("abc.txt");
//先要判断文件是否存在才能用下面 的方法
f.exists();//判断文件是否存在
f.isAbsolute();
f.isDirectory();
f.isFile();
f.isHidden();
}
public void mehtod2(){//删除文件夹或文件
File f=new File("f.txt");
boolean b=f.delete();
File f1=new File("abc");//当删除目录时,如果里面有文件,必须先删除里面的文件。
   File f2=new File("dhsagh");
   f2.deleteOnExit();//程序退出时一定要将该文件删除。出异常也不怕。
}
public void method1(){//创建文件夹
File f=new File("abc\\kk\\kdjlg\\sdhg");
//boolean b=f.mkdir();
boolean b=f.mkdirs();//创建多级目录。
}

public void mehtod() throws IOException{//创建文件
File f=new File("d://x.txt");//c如果不存在,该方法创建,如果存在,该方法不会创建,
                             //不会覆盖,输出流会覆盖。
boolean b=f.createNewFile();
System.out.println(b);
}
public void method(){
File f=new File("d://"+File.separator+"a.txt");//将指定文件封装成对象。
File f1=new File("d://","a.txt");
}
}


class TransDemo2{//将录入的数据,转换为大写,保存到文件中。
public void Trans(){
BufferedReader bufr=//一次读一行
new BufferedReader(new InputStreamReader(System.in));
   BufferedWriter bufw=null;
try {
bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E://JAVA代码/pack/d.txt")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
   String line=null;
   try {
while((line=bufr.readLine())!=null){
if(line.equals("over"))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();


}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   finally{
    try {
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    try {
bufw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   }
}
}






class TransStream{//转换流
public void Trans() throws IOException{

InputStream in=System.in;
InputStreamReader input=new InputStreamReader(in);//转换流将字节流转换为字符流

BufferedReader bufr=new BufferedReader(input);

OutputStream out=System.out;
OutputStreamWriter osw=new OutputStreamWriter(out);
BufferedWriter bufw=new BufferedWriter(osw);

String s=null;

while((s=bufr.readLine())!=null)
{
if("over".equals(s))
break;
bufw.write(s.toUpperCase());
bufw.newLine();
bufw.flush();
//System.out.println(s.toUpperCase());
}

bufw.close();
bufr.close();

}
}
class OtherCopy{
/*public String toUpperDemo() throws IOException{
String str=systemDemo();
while(str.equals("over"))
break;
return str.toUpperCase();

}*/
public void systemDemo() throws IOException{

int ch=0;
StringBuilder sb=new StringBuilder();
while(true){

ch=System.in.read();
if(ch==-1){
System.out.println(".......");
break;
}
if(ch=='\r')
continue;
if(ch=='\n'){
  String s=sb.toString();
  if(s.equals("over"))
  break;
  System.out.println(s.toUpperCase());
  sb.delete(0, s.length()); }
else
sb.append((char)ch);
}
}
}




class CopyPicture{
public void copy(){
FileOutputStream fos=null;
FileInputStream fis=null;
try {
fos=new FileOutputStream("D://me1.jpg");
fis=new FileInputStream("D://me.jpg");

byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}




class FileInputDemo{
public void in(){
FileInputStream fis=null;
try {
fis=new FileInputStream("E://JAVA代码/pack/C.txt");
byte[] by=new byte[10];
int i=0;
while((i=fis.read(by))!=-1){
String str=new String(by,0,i);
System.out.println(str);
}
/*int by=0;
while((by=fis.read())!=-1){
System.out.println((char)by);
}*/

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class FileOutputDemo{
public void out(){
try {
FileOutputStream fos=new FileOutputStream("E://JAVA代码/pack/C.txt");
byte[] buf="abcde".getBytes();
fos.write(buf);
fos.close();
   
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值