Java read/write

Source: Internet
Author: User

1. Obtain information entered by the console user

    Public String getinputmessage () throws ioexception ...{
        System. Out. println ("Enter your command ");
        Byte buffer [] = new byte [1024];
        Int COUNT = system. In. Read (buffer );
        Char [] CH = new char [count-2]; // the last two digits are the Terminator, delete not
        For (INT I = 0; I <count-2; I ++)
            Ch [I] = (char) buffer [I];
        String STR = new string (CH );
        Return STR;
    }
    The user input information can be returned. The disadvantage is that Chinese input is not supported and further improvement is needed.

    Ii. Copy an object
    1. copy a file as a file stream

    Public void copyfile (string SRC, string DEST) throwsioexception ...{
        Fileinputstream in = new fileinputstream (SRC );
        File file = new file (DEST );
        If (! File. exists ())
            File. createnewfile ();
        Fileoutputstream out = new fileoutputstream (File );
        Int C;
        Byte buffer [] = new byte [1024];
        While (C = in. Read (buffer ))! =-1 )...{
            For (INT I = 0; I <C; I ++)
                Out. Write (buffer [I]);       
        }
        In. Close ();
        Out. Close ();
    }
    This method is tested and supports Chinese processing and can be copied to multiple formats, such as txt, XML, JPG, and Doc.

    3. Write files

    1. Use printstream to write files


    Public void printstreamdemo ()...{
        Try ...{
            Fileoutputstream out = new fileoutputstream ("D:/test.txt ");
            Printstream P = new printstream (out );
            For (INT I = 0; I <10; I ++)
                P. println ("this is" + I + "line ");
        } Catch (filenotfoundexception e )...{
            E. printstacktrace ();
        }
    }
    2. Use stringbuffer to write files
Public void stringbufferdemo () throws ioexception ......{
        File file = new file ("/root/SMS. log ");
        If (! File. exists ())
            File. createnewfile ();
        Fileoutputstream out = newfileoutputstream (file, true );       
        For (INT I = 0; I <10000; I ++ )......{
            Stringbuffer sb = new stringbuffer ();
            SB. append ("This is the first line" + I + ": the methods described above are not used. Why is it always a strange question ");
            Out. Write (sb. tostring (). getbytes ("UTF-8 "));
        }       
        Out. Close ();
    }
    This method can be used to set the encoding to effectively solve Chinese problems.
Iv. File rename
    
    Public void renamefile (string path, string oldname, stringnewname )...{
        If (! Oldname. Equals (newname)... {// It is necessary to rename a new file name that is different from the previous one.
            

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.