Java Read and write files

Source: Internet
Author: User
Tags readfile

Read the file

 packagetool;Importjava.io.BufferedReader;Importjava.io.BufferedWriter;Importjava.io.File;Importjava.io.FileOutputStream;Importjava.io.FileReader;Importjava.io.IOException;Importjava.io.OutputStreamWriter; public classReadFile { public Static voidMain (string[] Args)throwsIOException {File F=NewFile ("f:/mernis/mernis.txt"); intCount=0; if(f.exists ()) {filereader Read=NewFileReader (f); BufferedReader Buff=NewBufferedReader (read); BooleanIsend=false; intI=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 blockE.printstacktrace (); }} System.out.println ("file total" +count+ "line");                             Buff.close (); }         Else{System.out.println ("the file you entered does not exist, please use it correctly:"); }              }}
Readfile.java

Read and write files

 packagetool;Importjava.io.BufferedReader;Importjava.io.BufferedWriter;Importjava.io.File;Importjava.io.FileOutputStream;Importjava.io.FileReader;Importjava.io.IOException;Importjava.io.OutputStreamWriter; public classTransferfile { public Static voidMain (string[] Args)throwsIOException {File F=NewFile ("f:/mernis/data_dump.sql"); File FileWrite=NewFile ("f:/mernis/mernis.txt"); intCount=0; if(f.exists ()) {filereader Read=NewFileReader (f); BufferedReader Buff=NewBufferedReader (read); BufferedWriter out=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (filewrite,true))); BooleanIsend=false; intI=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 blockE.printstacktrace (); }} System.out.println ("file total" +count+ "line");                   Buff.close ();                             Out.close (); }         Else{System.out.println ("the file you entered does not exist, please use it correctly:"); }              }}
Transferfile.java

Java read-write file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.