Archive for the ‘Java.io’ Category
Java Dates, Numbers and Currency
import java.util.*;
import java.text.*;
import java.text.NumberFormat;
import java.io.*;
public class DateFormatExample
{
public static void main (String args[])
{
Locale lo = new Locale(“pt”);
//Create a date object, then format it (short, medium,long,full), then print
Date d1 = new Date (10000L);
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);//Current date
System.out.println(“No Locale: “+ df.format(d1));
System.out.println(“”);
//Use the Locale class with a DateFormat Object
DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, lo);
System.out.println(“Yes Locale: “+df2.format(d1));
System.out.println(“Country Locale: “+lo.getDisplayCountry()); //No Country is passed to Locale constructor (see above), so this line is blank.g
System.out.println(“Language Locale: “+lo.getDisplayLanguage());
System.out.println(“”);
//NumberFormat
Float number = new Float(1000.12F);
Locale l = new Locale(“sp”);
NumberFormat f1 = NumberFormat.getInstance();
NumberFormat f2 = NumberFormat.getCurrencyInstance();
NumberFormat f3 = NumberFormat.getInstance(l);
NumberFormat f4 = NumberFormat.getCurrencyInstance(l);
System.out.println(f1.format(number));
System.out.println(f2.format(number));
System.out.println(f3.format(number));
System.out.println(f4.format(number));
//Other methods
f1.setMaximumFractionDigits(1);
System.out.println(“”);
System.out.println(“Maximum Fraction Digits for NF1: “+f1.getMaximumFractionDigits());
System.out.println(f1.format(number));
//Parse Integer only
try
{
//You get a Parse exception if you use getCurrencyInstance
NumberFormat nf2 = NumberFormat.getInstance();
nf2.setParseIntegerOnly(true);
System.out.println(nf2.parse(“123.123″));
}
catch (ParseException e)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bout));
System.out.println(bout.toString());
}
}
}
Java File Navigation: File, FileReader and FileWriter
import java.io.*;
public class FileTests
{
public static void main (String args[])
{
try
{
//FILEREADER, FILEWRITER
//Create File in directory
File directory = new File (“E:\\Java\\scjp\\Creating Files2″);
//You have to eiter manually create the directory in the Windows file system or run the code in the line below. Otherwise you will get an exception.
//directory.mkdir();
File f = new File (directory, “FileTest.txt”);
//The directory will not be created if it does not exist. Use f.mkDir() to create the directory.
if (f.exists())
{
System.out.println(“File already exists”);
}
else
{
f.createNewFile();
System.out.println(“File created: ” +f);
}
//Use FileWriter to write characters to File
FileWriter fw = new FileWriter (f);
fw.write(“First line test\r\nSecond line test”);
fw.write(“\r\nthird line test”);
fw.flush();
fw.close(); //cannot write any more characters because fw object is closed
//Use FileReader to read the characters entered
FileReader fr = new FileReader(f);
char charactersfromfile[] = new char[100]; //this can be as big as needed
int NumberOfCharacters = fr.read(charactersfromfile);
System.out.println(“Number of charachers = ” +NumberOfCharacters);
if (NumberOfCharacters > 0)
{
for (char c: charactersfromfile)
System.out.print(c);
}
else
{
System.out.println (“The file is empty!”);
}
fr.close();
File f2 = new File(directory, “FileTestRenamed.txt”);
f.renameTo(f2);
}
catch (Exception e)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bout));
System.out.println(bout.toString());
}
}
}
Java File Navigation: File, BufferedReader and BufferedWriter
import java.io.*;
public class FileBufferTests
{
public static void main (String [] args)
{
try
{
File directory = new File(“E:\\Java\\scjp\\Creating Files”);
File f = new File (directory, “FileBufferedTests.txt”);
directory.mkdir();
f.createNewFile();
FileReader fr = new FileReader(f);
FileWriter fw = new FileWriter(f);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(“this is a test – line 1″);
bw.newLine();
bw.write(“this is a test – line 2″);
bw.flush(); //this actually writes to the file
String s;
while((s = br.readLine()) !=null)
{
System.out.println(s);
}
br.close();
}
catch (Exception e)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bout));
System.out.println(bout.toString());
}
}
}
Java File Navigation: Directory Operations
import java.io.*;
public class DirectoryOperations
{
public static void main (String [] args)
{
//Delete the two files that were created in the previous examples
File directory = new File (“E:\\Java\\scjp\\Creating Files”);
File f1 = new File (directory, “FileTests.txt”);
File f2 = new File (directory, “FileBufferedTests.txt”);
//don’t need to run mkdir or createNewFile as they were run in the last examples
f1.delete();
f2.delete();
}
}



