* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* This class contains method to perform simple file operations like file reading and writing.
*/
public class FileOperations
{
public FileOperations()
{
}
/**
* Starting point of the class where execution begins
*
* @param args
* Command Line arguments
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
FileOperations fileOperations = new FileOperations();
String content = "Welcome to TechDive.in!";
String path = "D:\\techdive.txt";
System.out.println("Write the conent ->'" + content + "' to the file -> " + path);
fileOperations.writeToFile(content, path);
System.out.println("Read the file as String From -> " + path);
System.out.println(fileOperations.getFileContentAsString(path));
System.out.println("Read the file as Byte Array From -> " + path);
byte[] byteArray = fileOperations.getFileContentAsBytes(path);
for (int i = 0; i < byteArray.length; i++)
{
System.out.println("b[" + i + "] = " + ((int) byteArray[i] < 11 ? " " : "") + byteArray[i] + " , "
+ " character=(" + (char) byteArray[i] + ")");
}
}
/**
* This method returns a String by reading it from a BufferedReader
*
* @param bufferedReader
* BufferedReader
* @return Content as String
* @throws Exception
*/
public String getFileContentAsString(BufferedReader bufferedReader) throws Exception
{
BufferedReader buf = new BufferedReader(bufferedReader);
StringBuffer sBuf = new StringBuffer();
String s = null;
while ((s = buf.readLine()) != null)
{
sBuf.append(s);
}
return sBuf.toString();
}
/**
* This method reads from the given file and returns the file content as String
*
* @param File
* @return Content as String
* @throws Exception
*/
public String getFileContentAsString(File file) throws Exception
{
if (!file.exists())
{
throw new FileNotFoundException();
}
FileReader fileReader = new FileReader(file);
return getFileContentAsString(new BufferedReader(fileReader));
}
/**
* This method reads from the file in the given path and returns the file content as String
*
* @param path
* @return Content as String
* @throws Exception
*/
public String getFileContentAsString(String path) throws Exception
{
File file = new File(path);
return getFileContentAsString(file);
}
/**
* This method reads from the given Input Stream and returns the content as String
*
* @param Input
* Stream
* @return Content as String
* @throws Exception
*/
public String getInputStreamAsString(InputStream inputStream) throws Exception
{
return getFileContentAsString(new BufferedReader(new InputStreamReader(inputStream)));
}
/**
* This method writes the given String content in a file in the given path.
*
* @param textContent
* @param filePath
* @throws IOException
*/
public void writeToFile(String textContent, String filePath) throws IOException
{
writeToFile(textContent, new File(filePath));
}
/**
* This method writes the given String content in the given file.
*
* @param textContent
* Text Content
* @param file
* File
* @throws IOException
*/
public void writeToFile(String textContent, File file) throws IOException
{
FileOutputStream fO = new FileOutputStream(file);
fO.write(textContent.getBytes());
}
/**
* This method reads from a file in the given path and returns the file content as byte array.
*
* @param path
* File Path
* @return Context Byte Array
* @throws Exception
*/
public byte[] getFileContentAsBytes(String path) throws Exception
{
return getFileContentAsBytes(new File(path));
}
/**
* This method reads from the given file and returns the file content as byte array.
*
* @param file
* File
* @return Content Byte Array
* @throws Exception
*/
public byte[] getFileContentAsBytes(File file) throws Exception
{
FileInputStream fileInputStream = new FileInputStream(file);
byte[] byteArray = new byte[1024];
byte[] result = new byte[0];
int start = 0;
int end = byteArray.length;
int count = 0;
while ((count = fileInputStream.read(byteArray, start, end - 1)) != -1)
{
byte[] temp = new byte[result.length + count];
System.arraycopy(byteArray, 0, temp, result.length, count);
result = temp;
}
return result;
}
}
Output :
Read the file as String From -> D:\techdive.txt
Welcome to TechDive.in!
Read the file as Byte Array From -> D:\techdive.txt
b[0] = 87 , character=(W)
b[1] = 101 , character=(e)
b[2] = 108 , character=(l)
b[3] = 99 , character=(c)
b[4] = 111 , character=(o)
b[5] = 109 , character=(m)
b[6] = 101 , character=(e)
b[7] = 32 , character=( )
b[8] = 116 , character=(t)
b[9] = 111 , character=(o)
b[10] = 32 , character=( )
b[11] = 84 , character=(T)
b[12] = 101 , character=(e)
b[13] = 99 , character=(c)
b[14] = 104 , character=(h)
b[15] = 68 , character=(D)
b[16] = 105 , character=(i)
b[17] = 118 , character=(v)
b[18] = 101 , character=(e)
b[19] = 46 , character=(.)
b[20] = 105 , character=(i)
b[21] = 110 , character=(n)
b[22] = 33 , character=(!)