Skip to content

Commit c5f2afc

Browse files
committed
Replaced println commands by proper logging
1 parent c333ec4 commit c5f2afc

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

  • core-java-modules/core-java-nio-2/src/main/java/com/baeldung/lock

core-java-modules/core-java-nio-2/src/main/java/com/baeldung/lock/FileLocks.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@
1919
import java.util.concurrent.atomic.AtomicInteger;
2020
import java.util.stream.Stream;
2121

22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
2225
import com.google.common.base.Charsets;
2326

27+
2428
public class FileLocks {
29+
30+
private static Logger log = LoggerFactory.getLogger(FileLocks.class);
2531

2632
// Write locks
2733
/**
@@ -30,9 +36,9 @@ public class FileLocks {
3036
static void getExclusiveLockFromInputStream() throws IOException, NonWritableChannelException {
3137
Path path = Files.createTempFile("foo", "txt");
3238
try (FileInputStream fis = new FileInputStream(path.toFile()); FileLock lock = fis.getChannel().lock()) {
33-
System.out.println("This won't happen");
39+
log.debug("This won't happen");
3440
} catch (NonWritableChannelException e) {
35-
System.err.println(
41+
log.error(
3642
"The channel obtained through a FileInputStream isn't writable. "
3743
+ "You can't obtain an exclusive lock on it!");
3844
throw e;
@@ -52,7 +58,7 @@ static FileLock getExclusiveLockFromRandomAccessFile(long from, long size) throw
5258
try (RandomAccessFile file = new RandomAccessFile(path.toFile(), "rw");
5359
FileLock lock = file.getChannel().lock(from, size, false)) {
5460
if (lock.isValid()) {
55-
System.out.println("This is a valid exclusive lock");
61+
log.debug("This is a valid exclusive lock");
5662
return lock;
5763
}
5864
return null;
@@ -76,7 +82,7 @@ static FileLock getExclusiveLockFromFileChannelOpen(long from, long size) throws
7682
while (buffer.hasRemaining()) {
7783
channel.write(buffer, channel.size());
7884
}
79-
System.out.println("This was written to the file");
85+
log.debug("This was written to the file");
8086
Files.lines(path).forEach(System.out::println);
8187
return lock;
8288
}
@@ -90,9 +96,9 @@ static void getReadLockFromOutputStream(long from, long size) throws IOException
9096
Path path = Files.createTempFile("foo", "txt");
9197
try (FileOutputStream fis = new FileOutputStream(path.toFile());
9298
FileLock lock = fis.getChannel().lock(0, Long.MAX_VALUE, true)) {
93-
System.out.println("This won't happen");
99+
log.debug("This won't happen");
94100
} catch (NonReadableChannelException e) {
95-
System.err.println(
101+
log.error(
96102
"The channel obtained through a FileOutputStream isn't readable. "
97103
+ "You can't obtain an shared lock on it!");
98104
throw e;
@@ -112,7 +118,7 @@ static FileLock getReadLockFromInputStream(long from, long size) throws IOExcept
112118
try (FileInputStream fis = new FileInputStream(path.toFile());
113119
FileLock lock = fis.getChannel().lock(from, size, true)) {
114120
if (lock.isValid()) {
115-
System.out.println("This is a valid shared lock");
121+
log.debug("This is a valid shared lock");
116122
return lock;
117123
}
118124
return null;
@@ -132,12 +138,12 @@ static FileLock getReadLockFromRandomAccessFile(long from, long size) throws IOE
132138
try (RandomAccessFile file = new RandomAccessFile(path.toFile(), "r"); // could also be "rw", but "r" is sufficient for reading
133139
FileLock lock = file.getChannel().lock(from, size, true)) {
134140
if (lock.isValid()) {
135-
System.out.println("This is a valid shared lock");
141+
log.debug("This is a valid shared lock");
136142
return lock;
137143
}
138144
return null;
139145
} catch (Exception e) {
140-
System.out.println(e.getMessage());
146+
log.error(e.getMessage());
141147
}
142148
return null;
143149
}
@@ -227,7 +233,7 @@ public static void main(String[] args) throws InterruptedException, IOException
227233
lineCount.incrementAndGet();
228234
System.out.println(line);
229235
});
230-
System.out.println("Total lines written = " + lineCount.get());
236+
log.info("Total lines written = " + lineCount.get());
231237

232238
}
233239
}

0 commit comments

Comments
 (0)