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(' ');
}
//*********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
5、文件输入输出一个实例
package lipika;
import java.io.*;
public class main {
public static void main(String[] args) throws IOException {
//获得两个文件类
File inputFile = new File("c:\test1.txt");
File outputFile = new File("c:\test2.txt");
FileReader in = new FileReader(inputFile); //创建文件读入类
FileWriter out = new FileWriter(outputFile); //创建文件写出类
int c;
//如果到了文件尾,read()方法返回的数字是-1
while ((c = in.read()) != -1) out.write(c); //使用write()方法向文件写入信息
in.close(); //关闭文件读入类
out.close(); //关闭文件写出类
}
}
6、写一些数字到一个文本里面
package lipika;
import java.io.*;
public class main {
public static void main(String[] args) throws IOException {
File outputFile = new File("c:\test2.txt");
FileWriter out = new FileWriter(outputFile); //创建文件写出类
for(int i=10000000;i<=10031893;i++){
out.write(i + "
"); //使用write()方法向文件写入信息
}
out.close();
}
}
7、
package lipika;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class youhuiquan {
public static void main(String[] args) throws IOException {
File file=new File("c:\test2.txt");
FileInputStream fis=null;
Scanner input=null;
String str="INSERT INTO coupon(promoRuleId,couponNo,isSent,remainedTimes,STATUS,VERSION)"
+ "VALUES(37,123456789,0,1,1,0);";
fis=new FileInputStream(file);
input=new Scanner (fis);
StringBuffer nr=new StringBuffer();
File outputFile = new File("c:\test5.txt");
FileWriter out = new FileWriter(outputFile); //创建文件写出类
while(input.hasNext())
{
String hn = input.next();
out.write(str.replace("123456789", hn)+ "
"); //使用write()方法向文件写入信息
}
out.close();
}
}