How to get the absolute path location for this OutputStream?

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...?

Jon Skeet
people
quotationmark

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.

people

See more on this question at Stackoverflow