Created
October 3, 2012 18:14
-
-
Save zbigniewTomczak/3828722 to your computer and use it in GitHub Desktop.
OCJP IO Dates and Formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.text.DateFormat; | |
| import java.util.*; | |
| public class Dates { | |
| private static Date date = new Date((long) 1e12); | |
| private static Locale locale = new Locale("pl", "GB"); | |
| private static Calendar calendar = Calendar.getInstance(locale); | |
| private static DateFormat dateFormatter = DateFormat.getDateInstance( | |
| DateFormat.FULL, locale); | |
| public static void main(String[] args) { | |
| System.out.println(date); | |
| System.out.println(dateFormatter.format(date)); | |
| calendar.setTime(date); | |
| System.out.println("The week in " + locale.getDisplayCountry() | |
| + " locale starts at day: " + calendar.getFirstDayOfWeek()); | |
| // no such method | |
| // locale.setLocale(Locale.US); | |
| calendar.add(Calendar.DAY_OF_MONTH, 25); | |
| System.out.println(calendar.getTime()); | |
| calendar.setTime(date); | |
| calendar.roll(Calendar.DAY_OF_MONTH, 25); | |
| System.out.println(calendar.getTime()); | |
| calendar.add(Calendar.DAY_OF_MONTH, -3); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.*; | |
| public class FileIO { | |
| public static void unbufferedWrite(String filename) { | |
| File file = new File(filename); | |
| try { | |
| FileWriter writer = new FileWriter(file); | |
| writer.write("hello\nworld\n"); | |
| writer.flush(); | |
| writer.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void unbufferedRead(String filename) { | |
| File file = new File(filename); | |
| try { | |
| FileReader reader = new FileReader(file); | |
| System.out.println("encoding: " + reader.getEncoding()); | |
| char[] cbuf = new char[50]; | |
| System.out.println("read: " + reader.read(cbuf) + " bytes"); | |
| for (char c : cbuf) { | |
| System.out.print(c); | |
| } | |
| reader.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void bufferedWrite(String filename) { | |
| File file = new File(filename); | |
| FileWriter writer; | |
| try { | |
| writer = new FileWriter(file); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| return; | |
| } | |
| BufferedWriter bw = new BufferedWriter(writer); | |
| try { | |
| bw.write("hello"); | |
| bw.newLine(); | |
| bw.write("world"); | |
| bw.newLine(); | |
| bw.flush(); | |
| bw.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void bufferedRead(String filename) { | |
| File file = new File(filename); | |
| FileReader reader; | |
| try { | |
| reader = new FileReader(file); | |
| } catch (FileNotFoundException e) { | |
| e.printStackTrace(); | |
| return; | |
| } | |
| BufferedReader bf = new BufferedReader(reader); | |
| try { | |
| String line; | |
| while ((line = bf.readLine()) != null) { | |
| System.out.println(line); | |
| } | |
| bf.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void printWriterUse(String filename) { | |
| File file = new File(filename); | |
| PrintWriter printWriter; | |
| try { | |
| printWriter = new PrintWriter(file); | |
| } catch (FileNotFoundException e) { | |
| e.printStackTrace(); | |
| return; | |
| } | |
| printWriter.println("hello"); | |
| printWriter.format("world%d", 2); | |
| printWriter.flush(); | |
| printWriter.close(); | |
| } | |
| public static void fileOperations() { | |
| File dir = new File("newDir"); | |
| dir.mkdir(); | |
| try { | |
| System.out.println(dir.getCanonicalFile()); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| if (dir.isDirectory()) { | |
| System.out.println("is a directory"); | |
| } | |
| File file = new File(dir, "newFile"); | |
| try { | |
| if(file.createNewFile()) { | |
| System.out.println("File: " + file.getName() + " created"); | |
| } | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| File rFile = new File(dir, "newFile2"); | |
| if(file.renameTo(rFile)) { | |
| System.out.println("File: " + file.getName() + " renamed"); | |
| } | |
| System.out.println("Listng all files in "+dir.getAbsolutePath()); | |
| for (String f : dir.list()) { | |
| System.out.println(f); | |
| } | |
| System.out.println("Deleting " + dir.getName()); | |
| System.out.println("Deletion succeded: " + dir.delete()); | |
| System.out.println("Deleting all files in " + dir.getName()); | |
| for(File f : dir.listFiles()) { | |
| f.delete(); | |
| } | |
| System.out.println("Deleting " + dir.getName()); | |
| System.out.println("Deletion succeded: " + dir.delete()); | |
| } | |
| public static void main(String[] args) { | |
| String filename = "ocjpIOTest.txt"; | |
| unbufferedWrite(filename); | |
| unbufferedRead(filename); | |
| bufferedWrite(filename); | |
| printWriterUse(filename); | |
| bufferedRead(filename); | |
| fileOperations(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.IllegalFormatException; | |
| public class Formatting { | |
| public static void main(String[] args) { | |
| int i1 = -123; | |
| int i2 = 12345; | |
| System.out.printf(">%1$(7d< \n", i1); | |
| System.out.printf(">%0,7d< \n", i2); | |
| System.out.printf(">%+-7d< \n", i2); | |
| try { | |
| System.out.printf(">%d<", 12.3); | |
| } catch (IllegalFormatException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Locale; | |
| public class Locales { | |
| private static Locale localeBR = new Locale("pt", "BR"); // Brazil | |
| private static Locale localeDK = new Locale("dk", "DK"); // Denmark | |
| private static Locale localeIT = new Locale("it", "IT"); // Italy | |
| public static void main(String[] args) { | |
| System.out.println(localeBR.getDisplayCountry()); | |
| System.out.println(localeBR.getDisplayCountry(localeBR)); | |
| System.out.println(localeBR.getDisplayCountry(localeDK)); | |
| System.out.println(localeBR.getDisplayCountry(localeIT)); | |
| System.out.println(); | |
| System.out.println(localeBR.getDisplayLanguage()); | |
| System.out.println(localeBR.getDisplayLanguage(localeBR)); | |
| System.out.println(localeBR.getDisplayLanguage(localeDK)); | |
| System.out.println(localeBR.getDisplayLanguage(localeIT)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.text.NumberFormat; | |
| import java.util.Locale; | |
| public class NumberFormatting { | |
| private static Locale localeGB = new Locale("en", "GB"); | |
| private static Locale localeFR = Locale.FRANCE; | |
| public static void main(String[] args) { | |
| float num = 1345.789F; | |
| // NumberFormat is abstract | |
| // NumberFormat numberFormat = new NumberFormat(); | |
| NumberFormat numberFormat = NumberFormat.getInstance(); | |
| NumberFormat numberFormatGB = NumberFormat.getInstance(localeGB); | |
| NumberFormat numberFormatFR = NumberFormat.getInstance(localeFR); | |
| System.out.println(numberFormat.format(num)); | |
| System.out.println(numberFormatGB.format(num)); | |
| System.out.println(numberFormatFR.format(num)); | |
| NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); | |
| NumberFormat currencyFormatGB = NumberFormat | |
| .getCurrencyInstance(localeGB); | |
| NumberFormat currencyFormatFR = NumberFormat | |
| .getCurrencyInstance(localeFR); | |
| System.out.println(currencyFormat.format(num)); | |
| System.out.println(currencyFormatGB.format(num)); | |
| System.out.println(currencyFormatFR.format(num)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| public class Paterns { | |
| private static String string1 = "acz123u78 ou"; | |
| public static void main(String[] args) { | |
| Pattern p = Pattern.compile("\\d+"); | |
| Matcher m = p.matcher(string1); | |
| System.out.println("String: " + string1); | |
| printMatcher(m); | |
| printMatcher(Pattern.compile("\\w+").matcher(string1)); | |
| printMatcher(Pattern.compile("\\d\\s").matcher(string1)); | |
| printMatcher(Pattern.compile(".*?u").matcher(string1)); //reluctant | |
| printMatcher(Pattern.compile("o?").matcher(string1)); //greedy | |
| Scanner s = new Scanner(string1); | |
| System.out.println(s.findInLine(p)); | |
| System.out.println(s.findInLine(p)); | |
| System.out.println(s.findInLine(p)); | |
| } | |
| private static void printMatcher(Matcher m) { | |
| System.out.println("Pattern: " + m.pattern().pattern()); | |
| while(m.find()) { | |
| System.out.println("startsAt:" + m.start() + " matches:\"" + m.group() + "\""); | |
| } | |
| System.out.println(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.Console; | |
| import java.util.Arrays; | |
| public class UsingConsole { | |
| public static void main(String[] args) { | |
| Console c = System.console(); | |
| c.printf("Please enter your login:"); | |
| String login = c.readLine(); | |
| c.printf("Please enter you password:"); | |
| char[] pass = c.readPassword(); | |
| if (Arrays.equals(pass, "pass".toCharArray())) { | |
| c.printf("Welcome %s!\n", login); | |
| } else { | |
| c.printf("Sorry, you're not welcome here.\n"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment