Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a "how to read file" example #461

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ C - Adjusting Timeout and Socket Timeout
}
}

```
D - Reading a file
```java

SMBClient client = new SMBClient(config);

try (Connection connection = client.connect("SERVERNAME")) {
AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
Session session = connection.authenticate(ac);

// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare("SHARENAME")) {

//Opens the file
File smbFileRead = share.openFile("PATH\\FILENAME", EnumSet.of(AccessMask.FILE_READ_DATA), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null);

//Creates a buffered reader
BufferedReader reader = new BufferedReader(new InputStreamReader(smbFileRead.getInputStream()));

//Reads the content line by line
String content = reader.lines()
.collect(Collectors.joining(System.lineSeparator()))

//'content' holds the file's content
return content;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also nest the share.openFile in a try-block, as the File object needs a close to release.
And don't forget to properly close the smbFileRead.getInputStream()

}
}

```

== Frequently Asked Questions
Expand Down