How to get file
last modified time
package com.java2novice.files;
import java.io.File;
public class MyFileLastModified
{
public static void main(String a[])
{
File
file = new File("MyProp.xml");
System.out.println(file.lastModified());
}
}
How to convert byte
array to input stream
package com.java2novice.files;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ByteArrToInputStream
{
public static void main(String a[])
{
String
str = "converting to input stream";
byte[]
content = str.getBytes();
int size = content.length;
InputStream
is = null;
byte[]
b = new byte[size];
try
{
is
= new
ByteArrayInputStream(content);
is.read(b);
System.out.println("Data
Recovered: "+new String(b));
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(is
!= null) is.close();
}
catch (Exception ex){}
}
}
}
How to convert
input stream to reader or Buffered Reader
package com.java2novice.files;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MyIstoBr
{
public static void main(String a[])
{
InputStream
is = null;
BufferedReader
bfReader = null;
try
{
is
= new
FileInputStream("C:/sample.txt");
bfReader
= new
BufferedReader(new InputStreamReader(is));
String
temp = null;
while((temp
= bfReader.readLine()) != null)
{
System.out.println(temp);
}
}
catch (FileNotFoundException
e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
How to convert byte
array to reader or Buffered Reader
package com.java2novice.files;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MyByteArrayToReader
{
public static void main(String a[])
{
String str =
"converting to input stream"+"\n and this is second line";
byte[]
content = str.getBytes();
InputStream
is = null;
BufferedReader
bfReader = null;
try
{
is
= new
ByteArrayInputStream(content);
bfReader
= new
BufferedReader(new InputStreamReader(is));
String
temp = null;
while((temp
= bfReader.readLine()) != null)
{
System.out.println(temp);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(is
!= null) is.close();
}
catch (Exception ex){}
}
}
}
No comments:
Post a Comment