Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 48

Thread: Difficulty in creating/writing to a file (also, with reading file with data)

Threaded View

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Difficulty in creating/writing to a file (also, with reading file with data)

    Hi guys.
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package daos;
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import model.City;
    import model.YearData;
     
    import repositories.Repository;
     
    /**
     *
     * @author mga
     */
    public class DAOTextImpl implements DAOInterface {
          private ArrayList<YearData>yeardata;
     
        static final char DELIMITER=',';   
     
     
     
     
        @Override
        public Repository load(String filename) {
     
            Repository repository = new Repository();
     
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) { 
                String[] temp;
                String line = br.readLine();
                while(line!=null){
                    temp=line.split(Character.toString(DELIMITER));        
                    int id = Integer.parseInt(temp[0]);
                    String cityName = stripQuotes(temp[1]);
                    String country=stripQuotes(temp[2]);
                    City city=new City (id, cityName, country);
     
     
     
     
                      /*  line=br.readLine();
                        temp=line.split(Character.toString(DELIMITER));
                        String year=stripQuotes(temp[4]);
                        float precipitation=Float.parseFloat(temp[5]);
                        int maxTemp=Integer.parseInt(temp[6]);
                        int minTemp=Integer.parseInt(temp[7]);
                        int windSpeed=Integer.parseInt(temp[8]);
                        String windDirection=stripQuotes(temp[9]);
                     YearData yeardata=new YearData(year, precipitation, maxTemp, minTemp,windSpeed, windDirection); */
     
                   repository.getItem(id);
     
                 //   repository.add(city);
     
                    line = br.readLine();                
                }
     
     
     
     
     
     
     
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            return repository;
     
        }
     @Override
        public void store(String filename, Repository repository) {
                try (PrintWriter output = new PrintWriter(filename)) {
     
     
               output.print(repository.toString());
     
                output.close();
     
            } catch (FileNotFoundException ex) {
                Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
            }        
     
     
     
        private String stripQuotes(String str) {
            return str.substring(1, str.length()-1);
        }
    }

    I am having problems with the above code. Whenever I run the code, all I get is:

    [] I suspect that this is due to the fact that the array is empty....i.e. is not being read properly and so returns its default value, i.e. NULL.


    Whenever I try and open a file, I get NumberFormatException. This, I suspect is due to the fact that it contains illegal characters, such as a date, floats etc.

    I am also unsure as to how I would utilise an arraylist of objects, I have two (model) classes. One class contains an ArrayList, the second class contains the member variables which are to populate the ArrayList. However, I am utterly baffled as to what I do to implement the ArrayList with the code above. I cannot convert it to String, so this means that using something like:

    ArrayList<String> result = new ArrayList<>();

    try (FileReader f = new FileReader(filename)) {
    StringBuffer sb = new StringBuffer();
    while (f.ready()) {
    char c = (char) f.read();
    if (c == '\n') {
    result.add(sb.toString());
    sb = new StringBuffer();
    } else {
    sb.append(c);
    }
    }
    if (sb.length() > 0) {
    result.add(sb.toString());
    }
    }
    return result;
    For example, won't work due to the mismatch due to string/object.



    Cab anyone please advise me? I'm totally stuck
    Last edited by Tokugawa; November 18th, 2018 at 06:03 PM.

Similar Threads

  1. File reading/writing
    By Masic1990 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 10th, 2014, 02:02 AM
  2. Reading and writing file
    By FaisalBahadur in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 12th, 2014, 05:38 PM
  3. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  4. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  5. Reading a writing binary file
    By stu1811 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 30th, 2010, 12:58 PM