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

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中文件操作的更多相关文章

  1. java中文件操作的工具类

    代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...

  2. java中文件操作《一》

    在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.Fi ...

  3. java中文件的I/O操作

    java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...

  4. Java中创建操作文件和文件夹的工具类

    Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  5. java常见文件操作

    收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if ( ...

  6. JAVA中文件与Byte数组相互转换的方法

    JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...

  7. php中文件操作常用函数有哪些

    php中文件操作常用函数有哪些 一.总结 一句话总结:读写文件函数 判断文件或者目录是否存在函数 创建目录函数 file_exists() mkdir() file_get_content() fil ...

  8. python中文件操作的六种模式及对文件某一行进行修改的方法

    一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...

  9. python中文件操作的其他方法

    前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...

随机推荐

  1. codeforce 702C Cellular Network 二分答案

    http://codeforces.com/contest/702 题意:n个村庄,m个机站,问机站最短半径覆盖完所有村庄 思路:直接二分答案 二分太弱,调了半天..... // #pragma co ...

  2. (转载)OC学习篇之---类的初始化方法和点语法的使用

    昨天介绍了OC中类的定义和使用,今天我们来继续学习类的初始化方法和点语法的使用. 一.首先来看一下类的初始化方法 在Java中我们知道一个每个类都有构造方法,这里的初始化方法就是和构造方法一个概念的, ...

  3. mongoDB 3.0 安全权限访问控制 -摘自网络

    "E:\Program Files\MongoDB\Server\3.0\bin\mongod.exe" --logpath E:\mongodb\log\mongodblog.l ...

  4. 记录一次Android交叉编译ffmpeg排查错误

    Android版本手机直播引擎中,引用了libvlc开源库.项目接过来,发现编译脚本中使用了很多用户名下的绝对路径.项目相关人离职,导致这个脚本实际上已经废掉.而且不知道相关路径下有没有其他文件和第三 ...

  5. html学习笔记之position

    今天主要一直看试验position的各种属性,现在记录下来以此备忘. position有四种常有属性,分别是static,fixed.absolute,relative fixed就是相对于窗口的位置 ...

  6. 有趣的Node爬虫,数据导出成Excel

    最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...

  7. POJ 2185 Milking Grid(KMP)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4738   Accepted: 1978 Desc ...

  8. AutoCAD.NET二次开发:创建自定义菜单(COM)

    当我们要在CAD中创建自定菜单时,可以引用COM组件来实现. 下面是实现方式: 1.新建类库项目,并引用CAD目录(我这里用的是CAD2008)下的acdbmgd.dll.acmgd.dll,并将引用 ...

  9. AVCaptureSession 照相时获取 AVCaptureVideoPreviewLayer尺寸

    http://stackoverflow.com/questions/14153878/avcapturesession-preset-photo-and-avcapturevideopreviewl ...

  10. Listview上下滚动崩溃

    利用CursorAdapter在ListView中显示Cursor中不同同类型的item,加载均正常,滚动时报如下错误: 11-28 15:18:16.703: E/InputEventReceive ...