I haven't updated it for a long time, lazy, be played! 1. This is a basic file operation that implements the file reading, written to a digital operation; Import Java.io. *;
/ ** * @Author Gooing * / public class filew {private file f = new file ("d: //j2//a.txt"); public int getnum () {INT i = -1; try {string stri = ""; Bufferedreader in = new bufferedreader (new filereader (f)); while ((stri = in.readline ())! = Null) {i = integer.parseint (stri.trim ());} in.close ();} Catch (Exception E) {E.PrintStackTrace ();} return i;} public void setnum () {Int i = getnum (); i ; try {printwriter out = new brewriter (New Filewriter f, false))); Out.write (String.Valueof (i)); / / may be the reason for the encoding It is string out.close ();} catch (exception e) {E.PrintStackTrace ();}} public static void main (String [] args) {filew frW = new filew (); for (int i = 0; i <100; i ) {frw.setnum (); System.out.println (frw.getnum ());}}} 2, here is an example of learning factory model //garden.java implements an abstract garden factory, you can get the center of the garden and surrounding plants Of course, for different gardens, the center plant and the surrounding plants are different package pkgfactory;
/ ** * @author gooing TODO To change the template for this generated type * comment go to Window - Preferences - Java - Code Style - Code * Templates * / public abstract class Garden {public abstract Plant getCenter (); public abstract Plant getBorder ();
Public void memo () {system.out.println ("centerplant:" getcenter (). getname ()); system.out.println ("Borderplant:" getBorder (). getname ()); system.out. Println ("-----------------");}} // Plant.java Realize the basic abstraction of plants in the garden, only one plant is provided Name Attributes Package PkgFactory; Public Class Plant {PUBLIC Plant (String Name) {this.name = Name;} public string getname () {Return Name;}
Private string name;} // Vergarden.java and FlowerGarden.java realize a vegetable garden and garden package pkgfactory; public class veggarden extends garden {public plan pLANT getcenter () {return new place ("wheat");}
Public Plant GetBorder () {Return New Plant ("carrot");}} package pkgfactory; public class floweergarden extenship garden {public plan pLANTETCENTER () {Return New Plant ("rose");
Public Plant GetBorder () {Return New Plant ("juhua");}} // Gardener.java is a driver of this routine Package PkgFactory; Public Class Gardener {public static void main (string [] args) {Garden G1 = New flowergarden (); garden g2 = new veggarden (); g1.memo (); g2.memo ();}}