[[Printing Out A Subversion Repository Tree|<<]] | [[Managing repository With SVNKit|List]] | [[Printing Out Repository History|>>]]
== Printing Out File Contents ==
In this example we will study how to read contents of a file from a particular revision of a repository. The
first steps are just like in the previous example:
...
public class DisplayFile {
public static void main(String[] args) {
DAVRepositoryFactory.setup();
String url = "http://svn.svnkit.com/repos/svnkit/trunk";
String name = "anonymous";
String password = "anonymous";
String filePath = "www/license.html";
SVNRepository repository = null;
try {
repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
repository.setAuthenticationManager(authManager);
SVNNodeKind nodeKind = repository.checkPath(filePath, -1);
if (nodeKind == SVNNodeKind.NONE) {
System.err.println("There is no entry at '" + url + "'.");
System.exit(1);
} else if (nodeKind == SVNNodeKind.DIR) {
System.err.println("The entry at '" + url + "' is a directory while a file was expected.");
System.exit(1);
}
...
Now when we are sure that our url corresponds to a file we read its contents into an ''OutputStream'' and
collect its properties into a ''Map''.
...
Map fileProperties = new HashMap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
repository.getFile(filePath, -1, fileProperties, baos);
Then we find out whether the file is a text or binary file using the MIME type property. If it's a binary we don't
print its contents. We also print out versioned file properties.
...
String mimeType = (String) fileProperties.get(SVNProperty.MIME_TYPE);
boolean isTextType = SVNProperty.isTextMimeType(mimeType);
Iterator iterator = fileProperties.keySet().iterator();
while (iterator.hasNext()) {
String propertyName = (String) iterator.next();
String propertyValue = (String) fileProperties.get(propertyName);
System.out.println("File property: " + propertyName + "=" + propertyValue);
}
if (isTextType) {
System.out.println("File contents:");
System.out.println();
try {
baos.writeTo(System.out);
} catch (IOException ioe) {
ioe.printStackTrace();
}
} else {
System.out.println("Not a text file.");
}
...
And finally we run the program and have the following output in our console:
File property: svn:entry:revision=2802
File property: svn:entry:checksum=435f2f0d33d12907ddb6dfd611825ec9
File property: svn:wc:ra_dav:version-url=/repos/svnkit/!svn/ver/2795/trunk/www/license.html
File property: svn:entry:last-author=alex
File property: svn:entry:committed-date=2006-11-13T21:34:27.908657Z
File property: svn:entry:committed-rev=2795
File contents:
SVNKit :: License
The TMate Open Source License.
......................................
---------------------------------------------
Repository latest revision: 2802
----
Download the [http://svn.svnkit.com/repos/svnkit/trunk/doc/examples/src/org/tmatesoft/svn/examples/repository/DisplayFile.java example program source code].
[[Printing Out A Subversion Repository Tree|<<]] | [[Managing repository With SVNKit|List]] | [[Printing Out Repository History|>>]]