Summarize some of the basic elements of the Javaio reviewed today

Source: Internet
Author: User


1. Look at the Java programming idea IO This part of the time encountered a topic, need to use the string inversion, so recorded

Package com.jww.china.app.test.all.io;/** * @ClassName: Stringreverseutil * @Description: Implementing your own Java string inversion class * @author dimension * @d  ATE August 29, 2015 PM 7:35:11 *  */public class Stringreverseutil {public static void main (string[] args) {String s= ' I love Java "; System.out.println (ReverseString (s));} protected static string ReverseString (string s) {char[] C=s.tochararray (); int Begin=0;int end=c.length-1;// System.out.println (Swapchar (c, begin, end)), Swapchar (c, Begin, end), for (int i=0;i<end;i++) {if (c[i]== ') {Swapchar (c, Begin, i-1); begin=i+1;}} return new String (c);} /** * @Title: Swapchar * @Description: Converts the entire string to char, reverses the entire char * @param @param c* @param @param begin* @param @param end    * @return void    return type * */protected static String Swapchar (char[] c, int begin, int end) {while (begin<end) {Char temp=c[begin];c[begin]=c[end];c[end]=temp;begin++;end--;} return new String (c);}}


2. Basic file output

Package Com.jww.china.app.test.all.io;import Java.io.bufferedreader;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.filereader;import Java.io.ioexception;import Java.io.PrintWriter; Import java.io.stringreader;/** * @ClassName: Basicfileoutput * @Description: Read from one file, then write to another file, basic file output * @author dimension * @date August 29, 2015 PM 8:51:53 * */public class Basicfileoutput {public static void main (string[] args) throws IOException {WriteFile ();} private static void WriteFile () throws Ioexception{bufferedreader in=new BufferedReader (New StringReader (ReadFile ())); PrintWriter printwriter=new PrintWriter (New File ("D:/b.txt")); String s;int Linecount=0;while ((S=in.readline ())!=null) {printwriter.println (++linecount+ ":" +s);}    Printwriter.close ();} private static String ReadFile () throws Ioexception{bufferedreader b=new BufferedReader (New FileReader ("D:/a.txt")); StringBuilder sb=new StringBuilder (); String Str;while ((Str=b.readline ())!=null) {sb.append (str+ "\ n");} B.close (); return SB. toString ();}} 

3. Buffered input file

Package Com.jww.china.app.test.all.io;import Java.io.bufferedreader;import Java.io.filenotfoundexception;import Java.io.filereader;import Java.io.ioexception;import Java.lang.reflect.array;import Java.util.ArrayList;import Java.util.arrays;import Java.util.collections;import Java.util.linkedlist;import Java.util.List;import java.util.scanner;/** * @ClassName: Bufferediputfile * @Description: Buffered input file, basic file input * @author dimension * @date August 29, 2015 5:57 : */public class Basicfileinput {public static void main (string[] args) throws IOException {reverserstring ();//revers    Estr ();//system.out.println (Bufferedinputfile ("D:/a.txt")); BufferedReader ("D:/a.txt");}     /** * @Title: REVERSESTR * @Description: No space jdk comes with method inversion String * @param * @return void return type *    */private static void Reversestr () {Scanner s=new Scanner (system.in);    String Str=s.nextline ();    System.out.println (new StringBuilder (str). reverse (). toString ()); System.out.println (New StringreversEutil (). reversestring (str). toString ()); }/** * @Title: reverserstring * @Description: There is a space string, reverse participle, jdk comes with method * @param * @return void return type * */private Stati c void Reverserstring () {Scanner s=new Scanner (system.in); String String=s.nextline (); String[] Str=string.split (""); List<string> list=new arraylist<string> (); list=arrays.aslist (str); Collections.reverse (list), for (String l:list) {System.out.print (L + "");}} /** * @Title: Bufferedinputfile * @Description: Use BufferedReader to enter a file * @param @param filename* @param @return * @param @ Throws FileNotFoundException Set file * @return string return type * @throws */private static string Bufferedinputfile (string f Ilename) throws Filenotfoundexception{bufferedreader read =new BufferedReader (new FileReader (FileName)); String s; StringBuilder sb=new StringBuilder (); try {while ((S=read.readline ())!=null) {sb.append (s+ "\ n");} Read.close ();} catch (IOException e) {e.printstacktrace ();} return sb.tostring ();} /** * @Title: BufferedReader * @DescRiption:todo (here is a sentence describing the function of this method) * @param @param filename* @param @throws ioexception settings file * @return void return type * @thro WS */private static void BufferedReader (String fileName) throws ioexception{BufferedReader read =new bufferedreader (new F          Ilereader (FileName));  Linkedlist<string> l=new linkedlist<string> (); String s; String S1; String s2 = null; while ((S=read.readline ())!=null) {s1=s; L.add (S1);} read.close (); for (int i = 0; i < l.size (); i++) {System.out.println (L.get (i). toString ()); System.out.println (New StringBuilder (L.get (i)). Reverse (). toString ()); }}}

4. Serialization and deserialization of a Java Pojo


Package Com.jww.china.app.test.all.serializable;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import java.io.objectoutputstream;/** * Test Serialization code * @author Administrator * */public class testserializable {public-static void main (string[] args) throws FileNotFoundException {person-p1=new person (); Person P2=new person (); P1.setage (12); P1.setname ("ww"); P2.setage (13); P2.setname ("Zy"); WriteObject (P1, p2); System.out.println (ReadObject ("D:/a.txt"));  }/** * @param p1 * @param p2 * @throws filenotfoundexception */private static void WriteObject (person P1, person P2) throws  FileNotFoundException {//serialization is output to TXT for the program so it is out of FileOutputStream out =new FileOutputStream (new File ("D:/a.txt")); try {//create write to Objectoutputstreamobjectoutputstream objectoutputstream=new ObjectOutputStream (out) in the specified out; o Bjectoutputstream.writeobject (p1); Objectoutputstream.writeobject (p2); Objectoutputstream.flush (); Out.flush (); Objectoutputstream.close (); Out.close (); System.out.println ("Serialization success!!!" ");} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} private static Object ReadObject (String path) {try {fileinputstream inputstream=new fileinputstream (path); O Bjectinputstream inputstream2=new ObjectInputStream (InputStream);//Stringtemp=inputstream2.readobject (). toString    ();    Object temp= inputstream2.readobject ();    Inputstream.close (); Inputstream2.close (); if (temp!=null) {System.out.println ("Deserialization succeeded!! "); return temp;}} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}    return null;} }

















Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summarize some of the basic elements of the Javaio reviewed today

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.