1、
import java.io.*;
//写进文档,然后又在显示器显示出来。
public class
fileinputstream{
public static void main(String[] args) throws IOException
{
DataOutputStream out = new DataOutputStream(new
FileOutputStream("dest.txt"));
//定义要保存的数据数组。
double[] prices = { 19.99,
9.99, 15.99 };
//将prices中的数据以Tab键为分割符保存在文件中。
for (int i = 0; i <
prices.length; i ++)
{
out.writeDouble(prices[i]);
out.writeChar(‘\t‘);
}
//*********Found********
out.close();
//
创建数据输入流,将上面保存的文件再次打开并读取。
DataInputStream in = new DataInputStream(new
FileInputStream("dest.txt"));
double price;
double total = 0.0;
for
(int i = 0; i < prices.length; i ++) {
//*********Found********
price =
in.readDouble();
in.readChar(); // 扔掉tab键
total = total +
price;
}
System.out.println("For a TOTAL of: $" +
total);
in.close();
}
}
2、
import java.io.File;
import java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
//把source.txt文件的内容复制到dest.txt内容
public class fileinput2{
public static void main(String[] args) throws
IOException{
File inputFile;
File outputFile;
FileInputStream
in;
FileOutputStream out;
int c;
inputFile = new
File("source.txt");
outputFile = new File("dest.txt");
in = new
FileInputStream(inputFile);
//*********Found********
out=new
FileOutputStream(outputFile);
while ((c = in.read()) !=
-1)
//*********Found********
out.write(c);
in.close();
out.close();
}
}
3、
import java.io.*;
public class fileinput3 {
public static void
main(String[] args) {
char[] charArray =
{‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘};
char c
;
try{
//*********Found********** 文件流和数据输出流有什么区别
DataOutputStream out =
new DataOutputStream(
new FileOutputStream("test.dat"));
for(int i =0;
i<charArray.length;
i++){
out.writeChar(charArray[i]);
}
//写进了文件,又读出来。
out.close();
DataInputStream
in = new DataInputStream(
//*********Found**********
new
FileInputStream("test.dat"));
while(in.available() !=
0){
c=in.readChar();
System.out.print(c+"
");
}
System.out.println();
//*********Found**********
in.close();
}catch(IOException
e){}
}
}
4、
import java.io.*;
public class fileinput4{
public static void
main(String[] args){
int[] intArray = {1,2,3,4,5};
int j
;
try{
DataOutputStream out = new DataOutputStream(
new
FileOutputStream("data.dat"));
for(j =0; j<intArray.length;
j++){
//*********Found********
out.writeInt(intArray[j]);
}
out.close();
DataInputStream in = new DataInputStream(
//*********Found********
new
FileInputStream("data.dat"));
while(in.available() != 0)
{
j =
in.readInt();
System.out.println(j);
}
in.close();
}
catch(IOException
e){}
}
}
------------------------------------------------------
总结:一个是FileOutputStream 与FileInputStream
另外一个是DataOutputStream与DataInputStream
原文地址:http://www.cnblogs.com/bluewelkin/p/3715691.html