|
Commons IO example source code file (description.xml)
The Commons IO description.xml source code
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<document>
<properties>
<title>User guide
<author email="dev@commons.apache.org">Commons Documentation Team
</properties>
<body>
<section name="User guide">
<p>
Commons-IO contains
<a href="#Utility classes">utility classes,
<a href="#Endian classes">endian classes,
<a href="#Line iterator">line iterator,
<a href="#File filters">file filters,
<a href="#File comparators">file comparators and
<a href="#Streams">stream implementations.
</p>
<p>
For a more detailed descriptions, take a look at the
<a href="api-release/index.html">javadocs.
</p>
</section>
<section name="Utility classes">
<subsection name="IOUtils">
<p>
<a href="api-release/index.html?org/apache/commons/io/IOUtils.html">IOUtils
contains utility methods dealing with reading, writing and copying.
The methods work on InputStream, OutputStream, Reader and Writer.
</p>
<p>
As an example, consider the task of reading bytes
from a URL, and printing them. This would typically done like this:
</p>
<source>
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
InputStreamReader inR = new InputStreamReader( in );
BufferedReader buf = new BufferedReader( inR );
String line;
while ( ( line = buf.readLine() ) != null ) {
System.out.println( line );
}
} finally {
in.close();
}</source>
<p>
With the IOUtils class, that could be done with:
</p>
<source>
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
System.out.println( IOUtils.toString( in ) );
} finally {
IOUtils.closeQuietly(in);
}</source>
<p>
In certain application domains, such IO operations are
common, and this class can save a great deal of time. And you can
rely on well-tested code.
</p>
<p>
For utility code such as this, flexibility and speed are of primary importance.
However you should also understand the limitations of this approach.
Using the above technique to read a 1GB file would result in an
attempt to create a 1GB String object!
</p>
</subsection>
<subsection name="FileUtils">
<p>
The <a href="api-release/index.html?org/apache/commons/io/FileUtils.html">FileUtils
class contains utility methods for working with File objects.
These include reading, writing, copying and comparing files.
</p>
<p>
For example to read an entire file line by line you could use:
</p>
<source>
File file = new File("/commons/io/project.properties");
List lines = FileUtils.readLines(file, "UTF-8");</source>
</subsection>
<subsection name="FilenameUtils">
<p>
The <a href="api-release/index.html?org/apache/commons/io/FilenameUtils.html">FilenameUtils
class contains utility methods for working with filenames <i>without
using File objects. The class aims to be consistent
between Unix and Windows, to aid transitions between these
environments (such as moving from development to production).
</p>
<p>
For example to normalize a filename removing double dot segments:
</p>
<source>
String filename = "C:/commons/io/../lang/project.xml";
String normalized = FilenameUtils.normalize(filename);
// result is "C:/commons/lang/project.xml"</source>
</subsection>
<subsection name="FileSystemUtils">
<p>
The <a href="api-release/index.html?org/apache/commons/io/FileSystemUtils.html">FileSystemUtils
class contains
utility methods for working with the file system
to access functionality not supported by the JDK.
Currently, the only method is to get the free space on a drive.
Note that this uses the command line, not native code.
</p>
<p>
For example to find the free space on a drive:
</p>
<source>
long freeSpace = FileSystemUtils.freeSpace("C:/");</source>
</subsection>
</section>
<section name="Endian classes">
<p>
Different computer architectures adopt different
conventions for byte ordering. In so-called
"Little Endian" architectures (eg Intel), the low-order
byte is stored in memory at the lowest address, and
subsequent bytes at higher addresses. For "Big Endian"
architectures (eg Motorola), the situation is reversed.
</p>
<p>
There are two classes in this package of relevance:
</p>
<ul>
<li>
The <a href="api-release/index.html?org/apache/commons/io/EndianUtils.html">EndianUtils
class contains static methods for swapping the Endian-ness
of Java primitives and streams.
</li>
<li>
The <a href="api-release/index.html?org/apache/commons/io/input/SwappedDataInputStream.html">SwappedDataInputStream
class is an implementation of the <code>DataInput interface. With
this, one can read data from files of non-native Endian-ness.
</li>
</ul>
<p>
For more information, see
<a
href="http://www.cs.umass.edu/~verts/cs32/endian.html">http://www.cs.umass.edu/~verts/cs32/endian.html</a>
</p>
</section>
<section name="Line iterator">
<p>
The <code>org.apache.commons.io.LineIterator class
provides a flexible way for working with a line-based file.
An instance can be created directly, or via factory methods on
<code>FileUtils or
Other Commons IO examples (source code examples)Here is a short list of links related to this Commons IO description.xml source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.