码迷,mamicode.com
首页 > 其他好文 > 详细

io流入门级别demo

时间:2017-08-22 00:20:24      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:java初学io流

import java.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.FileReader;

//import java.io.IOException;

//import java.io.InputStreamReader;

//import java.io.OutputStream;

//import java.io.OutputStreamWriter;

//import java.io.PrintStream;


public class IODemo {

public static void main(String[] args) throws IOException

{

test();

}

public static void test() throws IOException

{

sopln("--start--");

// fileWrite(newFile("D:\\JavaCode\\day30\\day30\\src\\com\\youma\\www\\dont.txt"));

// showAllFiles("D:\\JavaCode\\day13");

copyFile("D:\\JavaCode\\day30\\day30\\src\\com\\youma\\www\\dont.txt","D:\\JavaCode\\day30\\day30\\src\\com\\youma\\www\\do.txt");

// newFile("D:\\JavaCode\\day30\\day30\\src\\com\\youma\\www\\speak.txt");

sopln("--end--");

}

//  键盘输入文件内容

public static void fileWrite(File file) throws FileNotFoundException

{

BufferedReader fr = new BufferedReader(new InputStreamReader((System.in)));

OutputStreamWriter bw = new OutputStreamWriter(new FileOutputStream(file));

// File f = file;

String str = null;

try {

while((str =fr.readLine())!=null)

{

if(str.equals("over"))

break;

bw.write(str+"\r\n");

bw.flush();

}

fr.close();

bw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static File newFile(String dir) throws IOException

{

// String[] stri = di.split("\\.");

// sopln(stri.length);

// String str1 = stri[0];

// sopln(str1);

// String str2 = stri[1];

// sopln(str2);

File file = new File(dir);

if(!file.exists())

{

sopln("no file now set");

file.createNewFile();

}

return file;

}

public static void sopln(Object obj)

{

System.out.println(obj.toString());

}

public static void showAllFiles(String dir) throws IOException

{

File f = new File(dir);

if(!f.exists())

sopln("路径有误!");

else

{

File[] fil = f.listFiles();

for(File file:fil)

{

if(file.isDirectory())

showAllFiles(file.getPath());

else

sopln(file.getPath());

}

}

}

public static void copyFile(String str, String dir) throws IOException

{

File copy = newFile(dir);

File file = new File(str);

if(!file.exists())

sopln("原始路径有误!");

else

{

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(copy)));

String st = null;

while((st = br.readLine()) != null)

{

bw.write(st);

bw.newLine();


}

                        bw.flush();

br.close();

bw.close();

}

}

}


io流入门级别demo

标签:java初学io流

原文地址:http://13215514.blog.51cto.com/13205514/1958123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!