Different ways of creating files - java

There are methods for creating files in java.io.File (like createNewFile() or mkdir()). Are there other ways of creating files in Java SE using "standard" API?

When you create a FileOuputStream, the file is created, if it does not exist, although this is not guaranteed:
A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform.

FileOutputStream can be used to create a file as shown below
FileOutputStream fos = new FileOutputStream("myfile");
FileOutputStream fos = new FileOutputStream(new File("myfile"));

You can use PrintWriter in conjunction with FileWriter such as PrintWriter write = new PrintWriter(new FileWriter("FileName", false)); writing to blank file or BufferedWriter works with FileWriter as well, such as BufferedWriter writer = new BufferedWriter(new FileWriter("FileName"));

Related

do not the overwrite with BufferedWritter

I created an object of a class in java and wrote information of this object in a file with BufferedWriter.
But when I create a new object and write information of this in the file, I lose the previous information of previous object.
How can I write in the file with BufferedWriter without overwriting a file?
If you use the FileWriter class it allows you to specify if you want to overwrite or append to the file in the constructor.
If you would like to append to a file that already exists, you can use the following:
BufferedWriter bW = new BufferedWriter(new FileWriter(new File("file.txt"), true));
Java default is overwriting the file. You can specify that you wish to append to a file.
boolean append=true;
FileWriter writer = new FileWriter(new File("yourfile.txt"),append);
BufferedWriter w = new BufferedWriter(writer);
// do your writing stuff

Writing to Files : Printwriter Converting Forward Slash to Backslash

Why is Printwriter doing this?
File file = new File("/files/KA.txt");
writer = new PrintWriter(file);
writer.write("HELLO");
In the above code I keep getting an error that says :
java.io.FileNotFoundException: \files\KA.txt (The network path was not found)
Except this was not my specified path? How do I then specify a file to write - usually create a new file and write to this? It also throws errors if KA.txt is not present - I ideally want to create a new file and writer to it.
Thanks
I ideally want to create a new file and writer to it.
You can simply create a file ,
PrintWriter writer = new PrintWriter("name.txt", "UTF-8");
writer.println("text");
where UTF-8 is the file encoding. and write to the file , remember it overrides if the file exists with the same name
The problem is that the parent /files directory doesn't already exist, so you must create it beforehand, using File.mkdirs.
File file = new File("/files/KA.txt");
File parentFile = file.getParentFile();
parentFile.mkdirs();
PrintWriter writer = new PrintWriter(file);
writer.write("HELLO");
writer.close();

How to save Jmeter Variables to csv file

Does anyone knoe how to save specific Jmeter Variables into a csv file?
I have already tried this topic with no succes: Write extracted data to a file using jmeter and this code:
FileWriter fstream = new FileWriter("result.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(${account_id});
out.close();
Thank you.
Replace your out.write(${account_id}); stanza with out.write(vars.get("account_id"));
It is better to close fstream instance as well to avoid open handles lack
If you're going to reuse this file, i.e. store > 1 variable, add a separator, i.e. new line
Final code:
FileWriter fstream = new FileWriter("result.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("account_id"));
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();
See How to use BeanShell: JMeter's favorite built-in component for comprehensive information on Beanshell scripting
You can use this code in your BeanShellPostProcessor. It may help You.
String acid="${account_id}";
FileWriter fstream = new FileWriter("result.csv",true);
fstream.write(acid+"\n");
fstream.close();

How to write new line in Java FileOutputStream

I want to write a new line using a FileOutputStream; I have tried the following approaches, but none of them are working:
encfileout.write('\n');
encfileout.write("\n".getbytes());
encfileout.write(System.getProperty("line.separator").getBytes());
This should work. Probably you forgot to call encfileout.flush().
However this is not the preferred way to write texts. You should wrap your output stream with PrintWriter and enjoy its println() methods:
PrintWriter writer = new PrintWriter(new OutputStreamWriter(encfileout, charset));
Alternatively you can use FileWriter instead of FileOutputStream from the beginning:
FileWriter fw = new FileWriter("myfile");
PrintWriter writer = new PrintWriter(fw);
Now just call
writer.println();
And do not forget to call flush() and close() when you finish your job.
It could be a viewer problem... Try opening the file in EditPlus or Notepad++. Windows Notepad may not recognize the line feed of another operating system. In which program are you viewing the file now?
String lineSeparator = System.getProperty("line.separator");
<br>
fos.write(lineSeparator.getBytes());
To add a line break use
fileOutputStream.write(10);
here decimal value 10 represents newline in ASCII

Create File at user defined path

I am creating a file in java using
BufferedWriter out = new BufferedWriter(new FileWriter(FileName));
StringBuffer sb=new StringBuffer();
sb.append("\n");
sb.append("work");
out.write(sb.toString());
out.close();
But this file is getting created inside the bin folder of my server.I would like to create this file inside a user-defined folder.
How can it be achieved.
I would like to create this file inside a user-defined folder.
The simplest approach is to specify a fully qualified path name. You could select that as a File and build a new File relative to it:
File directory = new File("/home/jon/somewhere");
File fullPath = new File(directory, fileName);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
(new FileOutputStream(fullPath), charSet));
try {
writer.write("\n");
writer.write("work");
} finally {
writer.close();
}
Note:
I would suggest using a FileOutputStream wrapped in an OutputStreamWriter instead of using FileWriter, as you can't specify an encoding with FileWriter
Use a try/finally block (or try-with-resources in Java 7) so that you always close the writer even if there's an exception.
To create a file in a specific directory, you need to specify it in the file name.
Otherwise it will use the current working directory which is likely to be where the program was started from.
BTW: Unless you are using Java 1.4 or older, you can use StringBuilder instead of StringBuffer, although in this case PrintWriter would be even better.

Categories

Resources