Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class PlantCollection {
- private static boolean bflower=false;
- private static char timeOfLife[] ;
- private static char name[] ;
- private static char color[] ;
- public static char[] getTimeOfLife() {
- return timeOfLife;
- }
- public static void setTimeOfLife(char[] timeOfLife) {
- PlantCollection.timeOfLife = timeOfLife;
- }
- public static char[] getName() {
- return name;
- }
- public static void setName(char[] name) {
- PlantCollection.name = name;
- }
- public static char[] getColor() {
- return color;
- }
- public static void setColor(char[] color) {
- PlantCollection.color = color;
- }
- static String str;
- static String scolor;
- static String sname;
- public static void main(String[] args) throws IOException {
- ArrayList<Plant> list = new ArrayList<Plant>();
- list.add(new Flower("Red", 200));
- list.add(new Flower("Green", 250));
- list.add(new Flower("Yellow", 300));
- list.add(new Tree(100, 300));
- list.add(new Tree(200, 600));
- saveToXML(list);
- saveToCSV(list);
- System.out.println(readFromXML("C:\\file.xml"));
- str=new String (timeOfLife);
- scolor=new String(color);
- sname=new String(name);
- if(bflower==true){
- System.out.println(bflower);
- list.add(new Flower(scolor, sname, Integer.parseInt(str)));
- }
- }
- public static void saveToXML(List<Plant> list) throws IOException{
- StringBuffer bufXML = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- bufXML.append("\r\n<elements>");
- for(Plant plant : list){
- bufXML.append(plant.toXMLString());
- }
- bufXML.append("\r\n</elements>");
- FileOutputStream fosXML = new FileOutputStream("C:\\file.xml");
- fosXML.write(bufXML.toString().getBytes());
- fosXML.close();
- }
- public static void saveToCSV(List<Plant> list) throws IOException{
- StringBuffer bufCSV = new StringBuffer();
- for(Plant plant : list){
- bufCSV.append(plant.toCSVString());
- }
- FileOutputStream fosCSV = new FileOutputStream("C:\\file.csv");
- fosCSV.write(bufCSV.toString().getBytes());
- fosCSV.close();
- }
- public static String readFromXML (String filename)throws IOException{
- String text="";
- Scanner in = new Scanner(new File("C:\\file.xml"));
- while(in.hasNext())
- text += in.nextLine() + "\r\n";
- in.close();
- char color[] = new char[text.indexOf("</color>")-7 - text.indexOf("<color>")];
- text.getChars(text.indexOf("<color>")+7, text.indexOf("</color>"), color, 0);
- System.out.println(color);
- char name[] = new char[text.indexOf("</name>")-6 - text.indexOf("<name>")];
- text.getChars(text.indexOf("<name>")+6, text.indexOf("</name>"), name, 0);
- System.out.println(name);
- char timeOfLife[] = new char[text.indexOf("</timeOfLife>")-12 - text.indexOf("<timeOfLife>")];
- text.getChars(text.indexOf("<timeOfLife>")+12, text.indexOf("</timeOfLife>"), timeOfLife, 0);
- System.out.println(timeOfLife);
- if (text.contains("<Flower>"))
- bflower=true;
- setColor(color);
- setName(name);
- setTimeOfLife(timeOfLife);
- return text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment