Opening a file in using java - java

I'd like to have it so that my java executeble will open a text file up when a Jbutton is pressed.
I dont know where to start so if someone could just nudge me in right direction that would be great
when is searched for opening a file using java it was all about reading in data from a file

Opening a File Pointer:
File f = new File("/path/to/file.txt");
Opening a file input stream (For reading):
FileInputStream in = new FileInputStream("/path/to/file.txt");
Opening a file output stream (for writing):
FileOutputStream out = new FileOutputStream("/path/to/file.txt");
Please take better care to use the documentation and Google. Stack Overflow is not here to do your homework for you.
Edit:
To appease #Takendarkk, I've included this:
Random Access File (More advanced that the previous functions, it opens a file with the mannerisms which C/C++ would use.):
RandomAccessFile raf = new RandomAccessFile("/path/to/file.txt", "rw+");

Related

How to change a value of a file using Java IO function

Can anyone help me how to change a line in a file using java IO function. The file is located at SDCard. I have tried some solution from different blog, but failed.
I need to change one attribute wpa_oper_channel=1 to 2,3,4..... as user demand in the file sdcard/sample.txt.
I have also tried using SED command, but still not working. Please suggest if there any solution using java IO function.
The Command I have used using SED :
sed -i 's/wpa_oper_channel=[0-9]\\+/wpa_oper_channel=7/' sample.txt
If your configuration file is in the form of a Properties file. This means each line is in a format of key=value. You can easily read, modify and write it. Here is an example, I assume you have no problem with reading and writing a file through streams.
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"sample.txt");
InputStream is = new FileInputStream(file); // Read your existing file
Properties props = new Properties();
props.load(is);
props.setProperty("wpa_oper_channel", "4");
OutputStream out = new FileOutputStream(file); // Your output file stream
prop.store(output, null); // Save your properties file without header
By doing this, you may lose if there is another type of line like comments or else.
Update
I updated to source code. But still you need to get read and write permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I haven't test the code, It will work with some exception handling. If it is not work please specify your error.
You just have to
read the file's content into memory (there are millions of examples on how to do that with java),
change the content (finding the position could be done using e.g. indexOf("wpa_oper_channel="))
write the content back to the file (there are millions of examples for that too)
What android do you use?
From android 4.4+ user haven't access to edit and remove files from SD Card. You must copy them to external storage and use there. See this link - http://code.google.com/p/android/issues/detail?id=67570

Writing from console to text file, file data not saved

I am trying to save the content of the java console into a text file but each time I close the program the text file goes blank and rewrites to it. i.e. if I write to a file today, close the program and come back and run it again tomorrow, it has remembered the information written to it.
You want to open the OutputStream in append mode. Demo code:
PrintWriter out = new PrintWriter(
new FileOutputStream(new File(filename), true));
What you experience is the normal behavior when you write a stream to a file, and this is not specific to the Java API.

How do i delete a file? And file questions about limits? Java Android

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes()
fos.close();
Above is how i write my files. Now is there a limit to it? i'm asking this because i set some text to it that i get from my text view, then later, when i get the text from that file, it isn't the full text.Secondly, how would i delete a file when using the above method?
Well for the limitations of writing in file is depends on the Available Free Memory.
for deleting a file simply use delete method as below,
File fos = new File(FILENAME);
fos.delete();
You want Context.deleteFile(String filename) to delete the file. There shouldn't be any reason why the file is limited. Check your writing and reading code.

Recreating a file and writing new contents

I am trying to delete a line from a text file in java, I have read around and it seems that there are a couple of solutions: delete the file and recreate it and write to it, or write to a temp file and rename it. It seems like both solutions involve closing streams, I may be missing something simple here but how do I use a writer with a file after I close it, is there something I can do to get a new writer up and running right after recreating or renaming the file so that I can modify the new file?
For example:
I have a file movies.txt, I save all the lines in that file to a variable, close the streams on movies.txt, delete movies.txt and create movies.txt, how can I write to the new movies.txt after I closed the stream to it?
Or if I rename the file:
I have movies.txt, I create temp.txt and write everything from movies.txt to temp.txt, close streams on movies.txt, rename temp.txt to movies.txt, how do I write to movies.txt if the streams are closed?
Your temp file method might be easier.
Say you want to delete the n-th line from a file, here are the steps you want to follow.
Create a reader for the target file
Create a writer to a temp file
Read each line from source file and write to temp file <--- do this n-1 times
Skip n-th line
Read each line from source file and write to temp file till end of source file.
Close source file reader and temp file writer.
Delete source file and rename temp file to source file's name.
I'm thinking that you are thinking that if you reopened the file for writing, you would clobber its current contents.
That is generally true, but you can also open a file in write append mode; for example using this constructor.
FileOutputStream fos = new FileOutputStream("file.txt", true);
This causes output to be written to the end of a file, if it already exists.
(There are probably other ways to do this, but this is the simple way.)

Read / write program in Java using JFileChooser

How would I link the file choosen from a JFileChooser to a file and how would I convert it to string being able to display and edit it in a TextArea?
I have the GUI set up using swing, but the link between actionListener and the JFileChooser is not complete.
Any help would be much appreciated.
Code: http://pastebin.com/p3fb17Wi
EDIT: I found this program, that does pretty much what i wanted to, but it does not allow me to save the actual file : http://www.java-forums.org/new-java/8856-how-get-content-text-file-write-jtextarea.html
To be able to save the changes you have made, you will have to use a Save Dialog. In the example you have quoted, a File Open Dialog is used. They work in a similar way, all that you need to do is then get the file to which the user would like to store the changes made, open a stream to it and write the data back. This tutorial shows you how to use the various File Choosers.
All text components support a read(...) and write(...) method. So all you have to do is get the name of the File and create your FileReader or FileWriter and then invoke the method.
All the file chooser is used for is the get the File name to be used by the reader or writer. So the basic code would be:
File saveFile = chooser.getSelectedFile();
FileWriterr writerr = new FileWriter( saveFile );
textArea.write(writer)
Of course you will probably want to use a Buffered reader/writer.

Categories

Resources