java read and write file migration

package read_write;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
/**
 * Mainly solve the migration of large text files The problem of memory overflow caused by large, using reading and writing
 * @author zhao
 *
 */
public class Demo {

public static void main(String[] args) {
read_write("src/read_write/ChannelScanQuery.xml","src/read_write /ChannelScanQuery_write.xml");
}

/**
* Write every hundred lines from a file to the file in the directory specified by the second
parameter; * If the file in the directory specified by the second parameter already exists, then Overwrite the original file;
* @param filename
* @param filename
* @author zhao
*/
public static void read_write(String filename,String desXMLPath){
//File size
    long epgSize=0;
try {
RandomAccessFile br=new RandomAccessFile(filename,"rw");//Rw sees you here. If read-only, just write r
String str = null;
//Number of lines read, read 100 lines per round
int i=0;
//Number of rounds read
int num=0;
//Read every round The retrieved text
String EPGXMLStr="";
while ((str = br.readLine()) != null) {
str = new String(str.getBytes("ISO-8859-1"),"gb2312");
i++ ;
EPGXMLStr=EPGXMLStr+str+"\n";
if(i>=100){//Predetermined to read 100 lines per round
i=0;
num++;
System.out.println("The number of lines in the read EPG file (unit: 100 lines) "+num);


//The first round of reading, save in a newly created file
if(num==1){
           WriteFile_new(EPGXMLStr,desXMLPath);
}else{
WriteFile(EPGXMLStr,desXMLPath) ;

epgSize=epgSize+EPGXMLStr.length();
           EPGXMLStr="";
}
}
if(i>0){
epgSize=epgSize+EPGXMLStr.length();
if(num==0){
WriteFile_new(EPGXMLStr,desXMLPath);
}else{
WriteFile(EPGXMLStr,desXMLPath);
}

EPGXMLStr="";
}
br.close();
} catch (Exception e) {
System.out.println("读取到的EPG文件出错"+e.getMessage());
// TODO: handle exception
}
System.out.println("文件大小:"+epgSize);
}
/**
* 创建一个文件并向其写入一段文本
     * 写文件时转码为GB2312
     * @param StoreStr  文本
     * @param filename  地址
     * @param filename
     * @author zzb
     */
    public static void WriteFile_new(String StoreStr, String filename) {
    
    try {
        if(StoreStr == null || StoreStr.equals("")) {
        return;
        }
        
            FileOutputStream os = new FileOutputStream(filename,false);
            
            Writer out = new OutputStreamWriter(os, "GB2312");
            out.write(StoreStr);
            out.close();
            os.close();
            //log.info("文件保存成功");
        } catch (IOException ioe) {
        System.out.println("WriteFile, IOException: " + ioe
                    .getMessage());
        }
        
    }
    /**
     * 写文件时转码为GB2312
     * @param StoreStr
     * @param filename
     * @author zzb
     */
    public static void WriteFile(String StoreStr, String filename) {
    
    try {
        if(StoreStr == null || StoreStr.equals("")) {
        return;
        }
            //log.info("文件保存到:" + filename);
            FileOutputStream os = new FileOutputStream(filename,true);
            
            Writer out = new OutputStreamWriter(os, "GB2312");
            out.write(StoreStr);
            out.close();
            os.close();
            //log.info("文件保存成功");
        } catch (IOException ioe) {
        System.out.println("WriteFile, IOException: " + ioe
                    .getMessage());
        }
     
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326496854&siteId=291194637