FileInputStream,FileOutputStream,FileWriter,FileReader
很简单,你可以这样做,他的基础就是,你首先new一个文件流,也就是一个通往文件的通道
就像用自来水要水管一样。
FileInputStream fis=new FileInputStream();
然后,你把水管打开,就可以读文件了(水就来了)
FileReader fr=new FileReader(fis);// <=======括号里的参数就是在铺好的水管以后打开它,并依靠它运输水。呵呵,不知道这样子讲你明白么?
不明白的话,可以跟贴接着问
很简单,你可以这样做,他的基础就是,你首先new一个文件流,也就是一个通往文件的通道
就像用自来水要水管一样。
FileInputStream fis=new FileInputStream();
然后,你把水管打开,就可以读文件了(水就来了)
FileReader fr=new FileReader(fis);// <=======括号里的参数就是在铺好的水管以后打开它,并依靠它运输水。呵呵,不知道这样子讲你明白么?
不明白的话,可以跟贴接着问
1、 建立数据流的类
2、 用于序列化的类和接口
3、 用于文件的类和接口字节输入流的超类 InputStream
字节输出流的超类 OutputStream
字符输入流的超类 Reader
字符输出流的超类 Writer 字节流向字符流转换器
InputStreamReader 从一个字节流读取字节,然后把它转换为字符
OutputStreamWriter 把字符转换为一个字节编码,然后把它写进字节流文件
FileInputStream 和FileOutputStream对 文件的字节进行读写操作。
FileReader 和 FileWriter 对文件的字符进行读写。
RandomAccessFile 对文件的内容进行随机访问。对文件读写的操作:
//1.以行为单位从一个文件读取数据
BufferedReader in = new BufferedReader(new FileReader("F:\\nepalon\\TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
in.close();// 输出到文件
try{
BufferedReader in4 =new BufferedReader(new StringReader(s2));
PrintWriter out1 =new PrintWriter(new BufferedWriter(new FileWriter("F:\\nepalon\\TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
}
catch(EOFException ex){
System.out.println("End of stream");
}
public static void main(String[] arg) throws IOException {
byte[] somebyte = {
'a', 'b', 'c'};
String hello = "hello";
FileOutputStream out = new FileOutputStream("c:/data.tmp");
if (out != null) {
System.out.println("开始写入文件");
out.write(somebyte);
String str = new String(somebyte, 0);
System.out.println("输出字节:" + somebyte + "或者输出字符串:" + str);
for (int i = 0; i < hello.length(); i++) {
out.write(hello.charAt(i));
}
System.out.println("输出字符串hello:" + hello);
out.close();
}
}
}
import java.io.FileOutputStream;/**
* @author 崔占民
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class copy
{
final private static int INT_READLEN = 10240; public static void main(String[] args)
{
if (args.length == 0 || (args.length == 1 && !args[0].equals("/?")))
{
System.out.println("command syntax is invalidity!");
return;
}
else if (args[0].equals("/?"))
{
System.out.println("copy SrcFile DesFile");
System.out.println("SrcFile: Source File.");
System.out.println("DesFile: destination File.");
return;
}
copy cpy = new copy(args[0], args[1]);
} copy(String aStrSrcFile, String aStrDesFile)
{
if (aStrSrcFile.equals("") || aStrDesFile.equals(""))
{
System.out.println("please input SrcFile and DesFile!");
System.out.println("copy SrcFile DesFile");
return;
} FileInputStream in = null;
FileOutputStream out = null;
try
{
int bytes = 0;
byte[] bteFile = new byte[INT_READLEN];
in = new FileInputStream(aStrSrcFile);
out = new FileOutputStream(aStrDesFile); while ((bytes = in.read(bteFile)) != -1)
{
out.write(bteFile, 0, bytes);
}
System.out.println("File Copy finished!");
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
in.close();
out.close();
}
catch (Exception e)
{
System.out.println(e.toString());
return;
}
}
}
}//这是类似DOS的COPY命令的一个例子,你看看吧 ~~~~~
Creates a new FileReader, given the File to read from.
FileReader(FileDescriptor fd)
Creates a new FileReader, given the FileDescriptor to read from.
FileReader(String fileName)
Creates a new FileReader, given the name of the file to read from.