Reputation: 53
byte bWrite [] = {11,21,3,40,5};
OutputStream os = new FileOutputStream("test.txt");
System.out.println("Created successfully"+os.getAbsolutePath());
This is my code.I have to get the location for "test.txt".please check it out if it is some other way to implement...?
Upvotes: 4
Views: 12922
Reputation: 96
When I tried your code with FileOutputStream it didn't work at all. But I was successful with the following code.
OutputStream os = openFileOutput("test.txt", MODE_PRIVATE);
System.out.println("Created successfully" + getFilesDir().getAbsolutePath() + "/test.txt");
getFilesDir().getAbsolutePath() appears to get the absolute path to the default location for openFileOutput.
Upvotes: 0
Reputation: 31648
FileOUtputStream
also has File
constructor so you can use that.
File out = new File("test.txt");
OutputStream os = new FileOutputStream(out);
System.out.println("Created successfully "+out.getAbsolutePath());
Upvotes: 9
Reputation: 1499810
Getting this information after you've only got an OutputStream
variable feels like the wrong approach to me. After all, there's no guarantee that an OutputStream
is writing to a file at all - it could be a ByteArrayOutputStream
or writing to a socket. You can get this information before you create the FileOutputStream
though. For example:
File file = new File("test.txt");
System.out.println("Absolute path: " + file.getAbsolutePath());
Or
Path path = Paths.get("text.txt");
System.out.println("Absolute path: " + path.toAbsolutePath());
... then create the FileOutputStream
based on that.
Upvotes: 5
Reputation: 1029
Why don't you just put your desired absolute path as the parameter of the constructor of FileOutputStream?
Upvotes: 0