Questions tagged [filewriter]

Java built-in class that allows writing character files.

Public class FileWriter extends OutputStreamWriter.

Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.

Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileWriter (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

Since: JDK1.1

Source: Oracle

1082 questions
260
votes
5 answers

Create whole path automatically when writing to a new file

I want to write a new file with the FileWriter. I use it like this: FileWriter newJsp = new FileWriter("C:\\user\Desktop\dir1\dir2\filename.txt"); Now dir1 and dir2 currently don't exist. I want Java to create them automatically if they aren't…
user321068
44
votes
9 answers

Create a new line in Java's FileWriter

I have coded the following FileWriter: try { FileWriter writer = new FileWriter(new File("file.txt"), false); String sizeX = jTextField1.getText(); String sizeY = jTextField2.getText(); writer.write(sizeX); …
st.math
  • 748
  • 2
  • 6
  • 16
41
votes
4 answers

Java FileWriter with append mode

I'm currently using FileWriter to create and write to a file. Is there any way that I can write to the same file every time without deleting the contents in there? fout = new FileWriter( "Distribution_" + Double.toString(_lowerBound) + "_" +…
Progress Programmer
  • 6,276
  • 14
  • 47
  • 51
37
votes
7 answers

PrintWriter vs FileWriter in Java

Are PrintWriter and FileWriter in Java the same and no matter which one to use? So far I have used both because their results are the same. Is there some special cases where it makes sense to prefer one over the other? public static void…
ough
  • 499
  • 1
  • 6
  • 10
33
votes
5 answers

Overwriting txt file in java

The code I've written is supposed to overwrite over the contents of the selected text file, but it's appending it. What am I doing wrong exactly? File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt"); String source =…
Karthik Balakrishnan
  • 4,153
  • 6
  • 34
  • 65
31
votes
9 answers

flush in java.io.FileWriter

I have a question in my mind that, while writing into the file, before closing is done, should we include flush()??. If so what it will do exactly? dont streams auto flush?? EDIT: So flush what it actually do?
i2ijeya
  • 14,732
  • 16
  • 58
  • 72
22
votes
3 answers

Append data to existing file in HDFS Java

I'm having trouble to append data to an existing file in HDFS. I want that if the file exists then append a line, if not, create a new file with the name given. Here's my method to write into HDFS. if (!file.exists(path)){ …
kennechu
  • 1,334
  • 7
  • 21
  • 33
21
votes
7 answers

HTML5 offline storage. File storage? Directories and filesystem API

For storing data offline WebApp can use: session storage, "advanced version of cookies" key/value based Web Storage (AKA local/global/offline/DOM storage) sql-based Web SQL Database (deprecated) and Indexed Database API FileReader and FileWriter…
Vanuan
  • 25,939
  • 9
  • 90
  • 96
19
votes
6 answers

FileWrite BufferedWriter and PrintWriter combined

Ok so I am learning about I/O, and I found the following code in one of the slides. can someone please explain why there is a need to have a FileWrite, BufferedWriter and PrintWriter? I know BufferedWriter is to buffer the output and put it all at…
Ahoura Ghotbi
  • 2,776
  • 12
  • 34
  • 61
17
votes
3 answers

(java) Writing in file little endian

I'm trying to write TIFF IFDs, and I'm looking for a simple way to do the following (this code obviously is wrong but it gets the idea across of what I want): out.writeChar(12) (bytes 0-1) out.writeChar(259) (bytes 2-3) out.writeChar(3) (bytes…
Tony Stark
  • 21,996
  • 37
  • 90
  • 111
15
votes
1 answer

Which ArrayBufferView

I'm retrieving an ArrayBuffer over XHR and want to save it to the FileSystem API using a FileWriter. The FileWriter expects a Blob and the Blob constructor won't take an ArrayBuffer, it takes an ArrayBufferView. There are many ArrayBufferViews to…
Matthew
  • 9,723
  • 7
  • 34
  • 44
14
votes
4 answers

Unable to see file in Windows Explorer while it is visible in Android file browser

Through my Android program I wrote a file like this: String file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Files/hello.txt"; BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(str+"\n"); \\yeah…
Atul Goyal
  • 3,371
  • 4
  • 37
  • 55
14
votes
2 answers

Using PhoneGap FileWriter.write for "big" files

I have a problem in my PhoneGap app. I would like to write a file of 15 MB. If I try the OS pulls more and more memory and the app crashes without message. I can reproduce this on android and blackberry tablets. Is there a way to implement the…
4Str4ngeG4me
  • 526
  • 5
  • 18
14
votes
4 answers

Is it necessary to close a FileWriter, provided it is written through a BufferedWriter?

Consider a BufferedReader as below: writer = new BufferedWriter(new FileWriter(new File("File.txt"), true)); In this case at the end of the application, I am closing the writer with writer.close() Will this be enough? Won't that FileWriter created…
vivek_jonam
  • 2,945
  • 7
  • 29
  • 40
14
votes
6 answers

Modify the content of a file using Java

I want to delete some content of file using java program as below. Is this the write method to replace in the same file or it should be copied to the another file. But its deleting the all content of the file. class FileReplace { …
Adesh singh
  • 1,933
  • 9
  • 21
  • 35
1
2 3
72 73