java中文件操作

例一:从一个文件读入数据,然后写入另外一个文件

package lipika;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;



public class iofile {

public static void main (String[] arg ) throws IOException{

    FileInputStream f = new FileInputStream("c:\\123.txt");

    byte[] dw =new byte [f.available()];

    f.read(dw);

    FileOutputStream d = new FileOutputStream("c:\\456.txt");

d.write(dw);

System.out.print(d);

}

}

例二:生成150个随机数,写入文件当中

package lipika;



import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Random;





public class yhq {

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



    File outputFile = new File("c:\\test3.txt"); 

    FileWriter out = new FileWriter(outputFile);

    

   

    //生成随机函数

    for(int i=0;i<150;i++){

        int number = new Random().nextInt(8999) + 1000;

        String zuizhong=new String("sfj200"+number);

        out.write(zuizhong + "\r\n");          



    }

    out.close();

}    



}

例三:读取文件的数字,一行一行的读取,把这个数字替换掉某一个字符串中的某个数字,然后把新的字符串写入文件中

package lipika;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class yhqdaoru  {

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

        File file=new File("c:\\test3.txt");

        FileInputStream fis=null;

        Scanner input=null;

    String str="INSERT INTO coupon(promoRuleId,couponNo,isSent,remainedTimes,STATUS,VERSION)"

                + "VALUES(81,'123456789',0,1,1,0);";

    /*    String str="UPDATE coupon SET promoRuleId=79 WHERE couponNo='123456789';";    */

        

            fis=new FileInputStream(file);

            input=new Scanner (fis);            

    

    

        StringBuffer nr=new StringBuffer();

         File outputFile = new File("c:\\test4.txt"); 

         FileWriter out = new FileWriter(outputFile); 

        while(input.hasNext())

        {

            String hn = input.next();        

                out.write(str.replace("123456789", hn)+ "\r\n");   

        }

         out.flush();

            

            out.close();

        }    

        





  }

 例四:如何生成随机数,写入文件当中

package lipika;



import java.io.*;

import java.util.Random;



public class lipika {



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



  



    File outputFile = new File("c:\\test30.txt"); 

    FileWriter out = new FileWriter(outputFile); //�����ļ�д����

 

    /*for(int i=10031893;i<=10031963;i++){

        

        out.write(i + "\r\n");   //ʹ��write()�������ļ�д����Ϣ   

    }*/

    //生成随机函数

    for(int i=0;i<10000;i++){

        /*int number = new Random().nextInt(100000000) + 1;

        String zuizhong=new String("yhq20"+number);

        out.write(zuizhong + "\r\n");  */

    /* 实现方式一,生成100000000 1个亿到9亿9千万。。。的随机数*/        

        

        /*int number = new Random().nextInt(899999999)+100000000;

        //new Random().nextInt(899999999)应该是900000000等才会生成生成100000000 1个亿到9亿9千万

        out.write(number + "\r\n");  

    */

        /*实现方式二,*/

        long Temp; //不能设定为int,必须设定为long

        //产生 1个亿到9亿9千万的随机数

        Temp=Math.round(Math.random()*899999999+100000000);

        out.write(Temp + "\r\n");

        



    }

    out.close();

}    

}

例五:读取文件的某个数字,替换字符串(数据库用),然后写入新的文件

package lipika;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;





public class lipikadaoru {



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

        File file=new File("E:\\微信端30元1万张.txt");

        FileInputStream fis=null;

        Scanner input=null;

            String str="INSERT INTO gift_certificate ("

                    + "giftCertificateNo,purchaser,recipient,isSentByEmail,giftCertAmt,remainedAmt,giftType,STATUS,VERSION,"

+ "m1Amt,m2Amt,m3Amt,m4Amt,m5Amt,m6Amt,m7Amt,m8Amt,m9Amt,m10Amt,m11Amt,m12Amt,customerId,registerTime)"

                    + "VALUES('123456789','N/A','N/A',1,500,500,1,1,0,30,0,0,0,0,0,0,0,0,0,0,0,-2,'2014-10-09 14:00:00');"

                    ;

        try{

            fis=new FileInputStream(file);

            input=new Scanner (fis);

            

        }

        catch(IOException e){

            e.printStackTrace();

            

        }

        StringBuffer nr=new StringBuffer();

         File outputFile = new File("e:\\test30.txt"); 

         FileWriter out = new FileWriter(outputFile);

        while(input.hasNext()){

            String hn = input.next();                

        

            out.write(str.replace("123456789", hn)+ "\r\n");

            out.flush();

        }

        

                                      

    }



}

 

你可能感兴趣的:(java)