Billy Panthar
Billy Panthar

Reputation: 11

Java File Appender write to windows network share

I am setting up the log4j logger in my code using a fileappender (see code below)

FileAppender qappender = new FileAppender();
qappender.setFile("C:\logfile1.txt");

How do I get it to write a file to a public windows network share like \\server1\path1\path2\log.txt

Upvotes: 1

Views: 2466

Answers (3)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Instead of using an File, you can use an OutputStream opened from a URL which you can access files from the network.

URL url = new URL("file://server1/file.txt");
URLConnection conn = url.openConnection();
OutputStream out = conn.getOutputStream();

Upvotes: 0

meriton
meriton

Reputation: 70564

Use a UNC path?

qappender.setFile("\\\\server\\share\\logfile1.txt")

(In Java string literals, \\ is the escape sequence for the character \, so the above would write to the path \\server\share\logfile1.txt )

Upvotes: 2

adarshr
adarshr

Reputation: 62593

Map the network share to a virtual drive such as X:\path1\path2\log.txt and then try writing.

Upvotes: 2

Related Questions