Μάθημα 2 [10/11/2015] Ο κώδικας που ακολουθεί επιδεικνύει τη χρήση των συναρτήσεων εισόδου εξόδου για ανάγνωση / εγγραφή από / σε αρχείο κειμένου. Χρησιμοποιεί τις κλάσεις Point και Shape. import java.io.*; import java.util.arraylist; import java.util.stringtokenizer; class Shape { int linewidth; int linecolor; int linestyle; int fillcolor; int fillstyle; String filename; Shape () { filename = null; linewidth=0; linecolor=0; linestyle=0;
fillcolor=0; fillstyle=0; } Shape (int lw, int lc, int ls, int fc, int fs, String fname) { filename=fname; linewidth=lw; linecolor=lc; linestyle=ls; fillcolor=fc; fillstyle=fs; } void SetFileName (String name) { filename = name; } String GetFileName() { return filename; } void SetLineWidth (int lw) { linewidth=lw; } int GetLineWidth () { return linewidth; } void SetLineColor (int lc) { linecolor=lc; } int GetLineColor () { return linecolor; } void SetLineStyle (int ls) { linestyle=ls; }
int GetLineStyle () { return linestyle; } void SetFillColor (int fc) { fillcolor=fc; } int GetFillColor () { return fillcolor; } void SetFillStyle (int fs) { fillstyle=fs; } int GetFillStyle () { return fillstyle; } void SetShape (int lw, int lc, int ls, int fc, int fs) { linewidth=lw; linecolor=lc; linestyle=ls; fillcolor=fc; fillstyle=fs; } void PrintShapeInfo (){ System.out.println ("Linewidth "+GetLineWidth()); System.out.println ("LineStyle "+GetLineStyle()); System.out.println ("LineColor "+GetLineColor()); System.out.println ("FillStyle "+GetFillStyle()); System.out.println ("FillColor "+GetFillColor()); }
void SaveToFile () throws IOException { FileWriter f = null; PrintWriter out=null; try { f = new FileWriter (GetFileName()); out = new PrintWriter (f); out.println ("Linewidth "+GetLineWidth()); out.println ("LineStyle "+GetLineStyle()); out.println ("LineColor "+GetLineColor()); out.println ("FillStyle "+GetFillStyle()); out.println ("FillColor "+GetFillColor()); } finally{ if (out!=null) out.close(); if(f!=null) f.close(); }} void SaveToFile (String fname) throws IOException { FileWriter f = null; PrintWriter out=null;
try { f = new FileWriter (fname); out = new PrintWriter (f); out.println ("Linewidth "+GetLineWidth()); out.println ("LineStyle "+GetLineStyle()); out.println ("LineColor "+GetLineColor()); out.println ("FillStyle "+GetFillStyle()); out.println ("FillColor "+GetFillColor()); } finally{ if (out!=null) out.close(); if(f!=null) f.close(); }} void LoadFromFile () throws IOException { String line; int i=0; int values [] = new int [5]; FileReader f = null; BufferedReader in=null; try { f = new FileReader (GetFileName());
in = new BufferedReader (f); while ((line=in.readline())!=null) { finally{ StringTokenizer st = new StringTokenizer (line); st.nexttoken(); String value = st.nexttoken(); values[i] = Integer.parseInt(value); i++; }} if (in!=null) in.close(); if(f!=null) f.close(); } SetLineWidth(values[0]); SetLineStyle(values[1]); SetLineColor(values[2]); SetFillStyle(values[3]); SetFillColor(values[4]);} void LoadFromFile (String fname) throws IOException { String line; int i=0; int values [] = new int [5];
FileReader f = null; BufferedReader in=null; try { f = new FileReader (fname); in = new BufferedReader (f); while ((line=in.readline())!=null) { finally{ StringTokenizer st = new StringTokenizer (line); st.nexttoken(); String value = st.nexttoken(); values[i] = Integer.parseInt(value); i++; }} if (in!=null) in.close(); if(f!=null) f.close(); } SetLineWidth(values[0]); SetLineStyle(values[1]); SetLineColor(values[2]); SetFillStyle(values[3]); SetFillColor(values[4]);}}
class Point extends Shape { int xcoord; int ycoord; void SetXCoord (int x) { xcoord = x; } int GetXCoord () { return xcoord; } void SetYCoord (int y) { ycoord = y; } int GetYCoord () { return ycoord; } void SetCoords (int x, int y) { xcoord = x; ycoord = y;} Point (int x, int y) { super(); xcoord = x; ycoord = y;} Point (int lw, int lc, int ls, int fc, int fs, int x, int y, String name ) { super (lw, lc, ls, fc, fs, name); xcoord = x; ycoord = y; }
} public class OOShapes { static int SavePointList (ArrayList<Point> plist, String filename) throws IOException { int i, points=plist.size(); FileWriter f = null; PrintWriter out=null; if (points==0) { System.out.println("No points found!!"); return (-1);} try { f = new FileWriter (filename); out = new PrintWriter (f); for (i=0;i<points;i++) { out.println ("Linewidth "+plist.get(i).getlinewidth()); out.println ("LineStyle "+plist.get(i).getlinestyle()); out.println ("LineColor "+plist.get(i).getlinecolor());
out.println ("FillStyle "+plist.get(i).getfillstyle()); out.println ("FillColor "+plist.get(i).getfillcolor()); out.println ("FillStyle "+plist.get(i).getxcoord()); out.println ("FillColor "+plist.get(i).getycoord()); }} finally{ if (out!=null) out.close(); if(f!=null) f.close(); } return (0); } public static void main(string[] args) throws IOException{ Point p; ArrayList <Point> points = new ArrayList <> (30); for (int i=0;i<30;i++) { p = new Point (i,i); points.add(p); } SavePointList(points,"c:\\points.txt"); }}