Tutorials to .com

Tutorials to .com » Jsp » Skills » [Java] examples of the basic learning

[Java] examples of the basic learning

Print View , by: iSee ,Total views: 37 ,Word Count: 558 ,Date: Wed, 26 Aug 2009 Time: 10:05 PM

Not been updated for a long time, and lazy, the beat!
1, this is a basic file operations, realization of the document to read, write a number of operations
package trying;
import java.io. *;

/ **
* @ Author gooing
* /
public class FileRw (
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 PrintWriter (new BufferedWriter (new FileWriter (f, false)));
out.write (String.valueOf (i));
/ / May be encoded reasons, if the words directly into int, there will be windows coding java coding and confusion, so here is the String to write
out.close ();
) catch (Exception e) (
e.printStackTrace ();
)
)
public static void main (String [] args) (
FileRw frw = new FileRw ();
for (int i = 0; i <100; i + +) (
frw.setNum ();
System.out.println (frw.getNum ());
)
)
)

2, the following is an example of the learning factory model
/ / Garden.java to achieve an abstract garden plants, you can get from the center of the garden plants and surrounding plants;
/ / Of course, for different gardens, its central plants and surrounding vegetation is not the same
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 achieve the basic abstractions of plants in the garden, where plants provide only a name attribute
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 respectively, a vegetable garden and garden to achieve sub -
package pkgfactory;
public class VegGarden extends Garden (
public Plant getCenter () (
return new Plant ( "Wheat");
)

public Plant getBorder () (
return new Plant ( "Carrot");
)
)

package pkgfactory;
public class FlowerGarden extends Garden (
public Plant getCenter () (
return new Plant ( "Rose");
)

public Plant getBorder () (
return new Plant ( "JuHua");
)
)

/ / Gardener.java is the routine of a driver
package pkgfactory;
public class Gardener (
public static void main (String [] args) (
Garden g1 = new FlowerGarden ();
Garden g2 = new VegGarden ();
g1.memo ();
g2.memo ();
)
)





Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.