Rabu, 03 Juli 2013

MATERI STRUKTUR DATA (Lecture (SD-02) String & File Processing)


Characters

Characters are the fundamental of String & File
Character literals:
  1. Letters: ‘a’, ‘b’, ..., ‘z’
  2. Digits: ‘0’, ‘1’, ..., ‘z’
  3. Punctuation: ‘,’ , ‘;’ , ‘.’ , ‘?’
  4. Escaped Characters: 

    • Single Quote : ‘\’’
    • Double Quote : ‘\”’
    • Blackslash : ‘\\’
    • NewLine : ‘\n’
    • Return : ‘\r’
    • Tab : ‘\t’
    • Backspace : ‘\b’
Character StandarizationRepresent as ASCII standart: American Standard Code of Information Excange
Defined by integer representations as byte 2^8
  1. Uppercase: ‘A’=(char)65, ‘B’=(char)66, ..., ‘Z’=(char)90
  2. Lowercase : ‘a’=(char)97, ‘b’=(char)98, ..., ‘z’=(char)122
  3. Digits: ‘0’=(char)48, ‘1’=(char)2, ..., ‘9’=(char)57
  4. Punctuation: ‘ ’=(char)32, ‘!’=(char)33
  5. Escaped: ‘\n’=(char)10, ‘\r’=(char)33
String to Char
public static void main(String[] args) {
        char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
        String helloString = new String(helloArray);
        System.out.println(helloString);
     System.out.println(helloString.charAt(0));
    }

Byte to Char
public static void main(String[] args) {
  System.out.println((char)65);
System.out.println((byte)'A');
String s = "hello \n world !!";
System.out.println(s);
System.out.println(s.length());
    }

String Concatenation &Substring
public static void main(String[] args) {
String h = "hello";
String w = "world";
System.out.println(h.concat(w)); 
        String s = h+w;
        String sub = s.substring(0, 5);
        System.out.println(sub);
    }

String Instance Methode
Java Methode for processing String:
  1. Replace: replace, replaceAll
  2. Split: split, token, substring
  3. Case converting : upperCase, lowerCase
String replace
public static void main(String[] args) {
     String s = "I say, \t Welcome \n in Java Worlds \n My Friends";
       System.out.println(s);
       System.out.println(s.replace('s','S'));
       System.out.println(s.replaceAll("[\t\n]",""));
    }

String Split
String can be split use character splitter inside,
public static void main(String[] args) {
     String sentence = "Hello World";
        String[] words = sentence.split(" ");
        System.out.println(words[0]);
        System.out.println(words[0].charAt(0));    
}

String Token
public Map tokens(String str){
        HashMap result = new HashMap();
        String key;
        String value;
        StringTokenizer tokenizer = new StringTokenizer(str, " ");
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            int index = token.indexOf(':');
            key = token.substring(0, index);
            value = token.substring(index + 1);
            result.put(key,value); 
    }
        return result;
    }

String Token
public static void main(String[] args) {
        // test tokenize
        StringToken st= new StringToken();
        Map s = st.tokens("aku:mahasiswa kuliah:universitasTrunojoyo");
        System.out.println(s);
        Object status = s.get("aku");
        System.out.println(status);
        Object kampus = s.get("kuliah");
        System.out.println(kampus);
}

File Processing
Java IO and NIO are primary packages for file stream processing.
As Default methodes for read and write files only work for plain text, also for file which have decoding extention such as: pdf, doc, docx, ppt, ... etc, we can use others properietary java components such as: pdfbox(for pdf), apachePOI(for microsoft office, open office), etc.

File Processing - read file

 public String readFile(String fileInput){        
        String s="";
try{
            File f = new File(fileInput);
            FileInputStream in = new FileInputStream(f);
    int j = (int)f.length();
            char[] ch = new char[j];
            for(int i=0; i
                ch[i]=(char)in.read();
            }
            s = String.copyValueOf(ch);
        }
        catch(Exception e){
            System.out.println(e.toString());
        }        
        return s;
    }

File Processing - copy file
public void copyfile(String fs, String fd) throws IOException{
try{
            File f1 = new File(fs);
            File f2 = new File(fd);
            if(f2!=null){
                f2.delete();
            }
            InputStream in = new FileInputStream(f1);
            OutputStream out = new FileOutputStream(f2);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0){
            out.write(buf, 0, len);
            }
                in.close();
                out.close();
            System.out.println("File "+fs+" is copied to:"+ fd);
            }
       catch(FileNotFoundException ex){
        System.out.println(ex.getMessage() + " in the specified directory.");
}
    }

File Processing
public static void main(String[] args) {
// copy file
       Filerw f = new Filerw();
       try{
       f.copyfile("F:\\Teks.docx", "F:\\CopyOfTeks.docx");
       }
       catch(IOException e){
           System.out.println("error to copy file");
       }
// read file
      String s = f.readFile("F:\\jurnal.txt");
       System.out.print(s);
    }


Materi  ini di ajarkan di Universitas Trunojoyo Madura (UTM) Jurusan Teknik Informatika. Untuk Lebih Jelasnya Download saja langsung di bawah ini. Semoga bermanfaat ^_^

Download materi Disini










Kritik dan Saran layangkan ke Facebook dan Email: Hasan.kawaguchi24@gmail.com

Tidak ada komentar:

Posting Komentar