I did some reading on the API for File IO and read the following blog post: http://techknock.blogspot.com/2008/05/file-hadling-in-android.html. His code works fine when everything is in the same activity. But what I am trying to do is create an IOInterface class that I can use to open multiple databases to populate multiple lists.
ListA.java
public class ListA
{
public List<ClassA> list;
private final String DBA = "dbA";
private IOInterface database;
public List()
{
list = new ArrayList<ClassA>();
database = new IOInterface();
}
...
public void initListA() throws IOException
{
database.openForWriting(DBA);
String myStr = new String("content");
database.dos.writeBytes(myStr);
database.dos.flush();
database.dos.close();
}
}
IOInterface.java
public class IOInterface
{
public DataOutputStream dos;
private FileOutputStream fos;
public void openForWriting(String database)
{
try {
fos = openFileOutput(database, Content.MODE_PRIVATE);
dos = new DataOutputStream(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Eclipse underlines fos = openFileOutput(database, Content.MODE_PRIVATE);. With the comment that openFileOutput() does not exist. The resolution to this is to extend the IOInteface class as an Activity. I suppose then openFileOutput() is a method of an activity class.
So my question is how do accomplish what I am trying to do? Standard Java file io such as:
File fp = new File("database");
FileOutputStream fos = new FileOutputStream(fp);
does not work. It catches a FileNotFoundException. This has to be doable though. Any ideas?
Thanks,
Method openFileOutput() is contained in the Context class, so you can pass an instance of this class to your method that opens files. And you should always use methods from Context when you want to work with files.
You can read about using the internal and external storages in the development guide.
Related
I have a file object created in a class called ReadFiles.
public class ReadFiles {
File file = new File("google.csv");
}
When entered as above with a set "google.csv" the File creation works.
What I want to do is pass a String filename from another class StockBuySell such that it creates the File in ReadFiles is created based on the String filename. Here is what I tried:
public class ReadFiles {
String filename;
public ReadFiles(String filename) {
this.filename = filename;
}
File file = new File(filename);
}
In the other class in the same package:
public class StockBuySell {
ReadFiles googleData = new ReadFiles("google.csv");
}
I am given a NullPointerException. I believe that is because the file is not created by my method. What can I do? Thank you for your help.
Edit: I realized I was running into errors because of other methods related to reading the files. I ended up using hata's method to create a File. Thank you guys!
Is what you want to achieve like this?:
public class ReadFiles {
File file;
public ReadFiles(String filename) {
file = new File(filename);
}
}
The reason for your NullPointerException should be the field File file = new File(filename);. The filename is initially null before the Constructor method is called.
The order of initialization is a bit wonky; first all 'instance initializers' run (and expressions assigned to fields are part of this), and only then the constructor runs. Thus:
public class ReadFiles {
private final String filename;
private final File file;
public ReadFiles(String name) {
this.filename = filename;
this.file = new File(filename); // put it here.
}
}
There's all sorts of problems with this code, though. It's using old API, and it's got improper naming. (This code is reading 1 file, and yet is named 'ReadFiles'. Also, classes should not be named with a verb, it should describe what it represents, not what it does. It also has an additional, mostly useless field.
Fixing all that:
public class CsvReader {
private final Path file;
public CsvReader(String path) {
this.file = Paths.get(path);
}
public CsvReader(Path path) {
this.path = path;
}
}
and then we run into yet another problem: reading csvs sounds simple but it isn't. Use a library such as OpenCSV.
I had a programming assignment where i had to save separate files, one a log file and the other a dat file. Each one took an arraylist object and saved it in the file but each arraylist was different. I was under a time crunch so i ended up making separate functions for both but i'm wondering if there's a more modular way of doing it. So for example:
public void saveLog (ArrayList<A> objectArray, File fileName) throws IOException {
if (!fileName.exists()) {
logFile.createNewFile();
}
FileOutputStream fileoutput = new FileOutputStream(fileName);
ObjectOutputStream output = new ObjectOutputStream(fileoutput);
output.writeObject(objectArray);
output.close();
fileoutput.close();
}
Is there a way to recode this so it will also take ArrayList"<"B">" ? I tried using ArrayList"<"Object">" but that threw an error. Very new to java so sorry if this seems like a simple question.
Basically you need to accept an ArrayList which contains objects that can be serialized, this can easily be expressed with a wildcard, eg:
public void saveLog(ArrayList<? extends Serializable> objectArray, File fileName) {
..
}
Which means "accept an ArrayList of an unspecified type which implements Serializable interface", which is a requirement for the ObjectOutputStream.
You could make the method generic on A (and I would prefer the List interface). Also, I would prefer a try-with-resources Statement. Like
public <A> void saveLog(List<A> objectArray, File fileName)
throws IOException {
if (!fileName.exists()) {
logFile.createNewFile();
}
try (FileOutputStream fileoutput = new FileOutputStream(fileName);
ObjectOutputStream output = new ObjectOutputStream(fileoutput)) {
output.writeObject(objectArray);
}
}
I have the following class:
public class test{
private static final File testfile = new File("filename")//imaginary file name
private static BufferedWriter writer = null;
public void test1(){
writer = new OutStreamWriter(newFileOutputStream(testfile));
writer.write("hello");
writer.close();
}
public void test2(){
writer = new OutStreamWriter(newFileOutputStream(testfile));
writer.write("hello");
writer.close();
}
}
I want both of them to write to the same file whenever called and I create bufferedwriters inside each of the methods. However, once i call close on to the buffered writer, it cannot be opened again. How do I avoid this so I can call both methods multiple times?
I believe you had the same issue of this poster Java - Do not overwrite with bufferedwriter
Sanjay T. Sharma says:
"FileWriter takes an optional boolean argument which specifies whether it should append to or overwrite the existing content. Pass in true if you want to open the file for writing in append mode."
I'm using java.util.prefs.Preferences for application preferences.
And I need ability to edit those preferences manually.
Is it possible to store it into file instead of Windows Registry?
Or I should use another mechanism instead of java.util.prefs.Preferences?
If you want to continue using the Preferences API, but write to a file, you will need a new PreferencesFactory, as detailed in this SO post.
You are going to want to use the following two method :
Preferences.exportSubtree(OutputStream os)
and
Preferences.importPreferences(InputStream is)
This code should help you [http://java.sun.com/developer/technicalArticles/releases/preferences/]:
public class PrefSave {
private static final String PACKAGE = "/pl/test";
public static void main(String[] args) {
doThings(Preferences.systemRoot().node(PACKAGE));
doThings(Preferences.userRoot().node(PACKAGE));
}
public static void doThings(Preferences prefs) {
prefs.putBoolean("Key0", false);
prefs.put("Key1", "Value1");
prefs.putInt("Key2", 2);
Preferences grandparentPrefs = prefs.parent().parent();
grandparentPrefs.putDouble("ParentKey0", Math.E);
grandparentPrefs.putFloat("ParentKey1", (float) Math.PI);
grandparentPrefs.putLong("ParentKey2", Long.MAX_VALUE);
String fileNamePrefix = "System";
if (prefs.isUserNode()) {
fileNamePrefix = "User";
}
try {
OutputStream osTree = new BufferedOutputStream(
new FileOutputStream(fileNamePrefix + "Tree.xml"));
grandparentPrefs.exportSubtree(osTree);
osTree.close();
OutputStream osNode = new BufferedOutputStream(
new FileOutputStream(fileNamePrefix + "Node.xml"));
grandparentPrefs.exportNode(osNode);
osNode.close();
} catch (IOException ioEx) {
// ignore
} catch (BackingStoreException bsEx) {
// ignore too
}
}
Try the following class which allows you to use some simple put() and get() functions using a local configuration.xml file.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
public class SimpleProperties
{
private String propertiesFilePath;
private Properties properties;
public SimpleProperties() throws InvalidPropertiesFormatException, IOException
{
propertiesFilePath = "configuration.xml";
properties = new Properties();
try
{
properties.loadFromXML(new FileInputStream(propertiesFilePath));
} catch (InvalidPropertiesFormatException e)
{
}
}
public void put(String key, String value) throws FileNotFoundException, IOException
{
properties.setProperty(key, value);
store();
}
public String get(String key)
{
return properties.getProperty(key);
}
private void store() throws FileNotFoundException, IOException
{
String commentText = "Program parameters";
properties.storeToXML(new FileOutputStream(propertiesFilePath), commentText);
}
}
It is explained in another post, here
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close()
I think you can use property files instead. They are stored in the file system. You can define the path you want. And you can edit it by hand. See this question for more details.
A while back I had to come up with an implementation of the Preferences class that would read settings from but not write to the registry. I derived a ReadOnlyPreferences class from AbstractPreferences to accomplish this. Later, I needed this exact same functionality you require to go to/from files. I just extended my ReadOnlyPreferences class to override sync() and flush() to keep the file in sync. The cool part about this it would use the exact same logic to apply defaults to the values just like the usual use of the prefs since nothing actually existed in the registry to read. I kept the file in sync by using exportSubtree() and importPreferences() from the base class to do all the heavy lifting for me.
I am sorry I cannot post the code as I don't own it but I used the encrypted preferences stuff you can find at the following link as a start point. That's what I did and it took me about an hour to distill it down to just what I needed which was mainly throwing code away which is much easier than writing code! It is also published in Dr Dobbs at the following link if you don't want to click on the first one. I just never saw an easy place on the dobbs article to download the entire source. Regardless, the article is the best I've seen for extending the preferences stuff.
http://www.panix.com/~mito/articles/#ep
http://www.drdobbs.com/security/encrypted-preferences-in-java/184416587?pgno=4
I am having an annoying problem that I cannot seem to fix. I have a class named DirFormMgmt which manages two files; directories.txt and formats.txt. The problem is that when I instantiate an instance of this class when the program starts it clears everything that was in the files before it started, so for instance if I open the formats.txt file and type in ".avi" and run the program the text file will become blank. Here is the only code I have running in my class to isolate the issue and I am unsure of why this is happening:
At the top of my class I declare:
File dirFile = new File("directories.txt");
File formatFile = new File("formats.txt");
private BufferedReader dirReader;
private BufferedWriter dirWriter;
private BufferedReader formatReader;
private BufferedWriter formatWriter;
The constructor:
public DirFormMgmt() throws IOException {
checkFileExistence();
initReaderWriter();
}
The two methods called by the constructor:
public void checkFileExistence() throws IOException {
if (!dirFile.exists()) {
dirFile.createNewFile();
}
if (!formatFile.exists()) {
formatFile.createNewFile();
}
}
and:
public void initReaderWriter() throws IOException {
dirReader = new BufferedReader(new FileReader(dirFile));
dirWriter = new BufferedWriter(new FileWriter(dirFile));
formatReader = new BufferedReader(new FileReader(formatFile));
formatWriter = new BufferedWriter(new FileWriter(formatFile));
}
I checked to see if the createNewFile() methods were being called but they were not, so the problem must be with my initReaderWriter() method, but I am unsure of what is wrong, any help is greatly appreciated!
Use new FileWriter(file, true) to append content to an existing file. Otherwise, the contents will be overwritten.
You can't read and write to the same file like this. The FileWriter constructor is used to write to a file and replace all its contents.
EIther read everything in memory, and then write to the file, or write to another temp file, and remove the original file and rename the temp file when you're done.