首页 > 代码库 > java读写文件

java读写文件

读文件

技术分享
package tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class ReadFile {

    public static void main(String[] args) throws IOException
    {
         File f=new File("F:/mernis/mernis.txt");
         
          int count=0;
          if(f.exists())
              { 
                   FileReader read=new FileReader(f);
                   BufferedReader buff=new BufferedReader(read);
                
                   boolean isEnd=false;
                   int i=0;
                   while(!isEnd)
                   {
                    String line;
                    i++;
                    //for (int i = 0; i <1000; i++) {
                        
                    //}
                    try {
                        line = buff.readLine();
                        if(line==null)
                            {
                              isEnd=true;
                            }
                        else
                            {
                                //System.out.println(line);
                                    if (i>=1&&i<=100) 
                                    {
                                        
                                        System.out.println(i+"##"+line);
                                    }
                             count++;
                            }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                   }
                   
                   System.out.println("文件共有"+count+"行");
                   buff.close();
               
              }
         else
              {
               System.out.println("你输入的文件不存在,请正确使用:");
              }
          
    }
}
ReadFile.java

读写文件

技术分享
package tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class TransferFile {

    public static void main(String[] args) throws IOException
    {
         File f=new File("F:/mernis/data_dump.sql");
         File filewrite=new File("F:/mernis/mernis.txt");
          int count=0;
          if(f.exists())
              { 
                   FileReader read=new FileReader(f);
                   BufferedReader buff=new BufferedReader(read);
                   BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filewrite, true)));
                
                   
                   boolean isEnd=false;
                   int i=0;
                   while(!isEnd)
                   {
                    String line;
                    i++;
                    //for (int i = 0; i <1000; i++) {
                        
                    //}
                    try {
                        line = buff.readLine();
                        if(line==null)
                            {
                              isEnd=true;
                            }
                        else
                            {
                                //System.out.println(line);
                                    if (i>=76&&i<=49611784) 
                                    {
                                        out.write(line+"\r\n");
                                        //System.out.println(i);
                                        //System.out.println(i+"##"+line);
                                    }
                             count++;
                            }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                   }
                   
                   System.out.println("文件共有"+count+"行");
                   buff.close();
                   out.close();
               
              }
         else
              {
               System.out.println("你输入的文件不存在,请正确使用:");
              }
          
    }
}
TransferFile.java

 

java读写文件