April 24th, 2008

Java covert Linux path to Windows path

No Comments », Java, by nolifelover

สำหรับช่วงนี้ห้องผมจะมีโจทย์จากกอาจาร์มาประมาณ 1 ข้อต่อวันครับเพื่อฝึกทักษะการเขียนโปรแกรมของพวกเราหลังจากที่ ได้เขียน Android Application ไม่เข้าตากรรมการ สำหรับโจทย์วันแรกคือการเขียนเจ้า Java เพื่อแปลง path ของ linux ไปเป็น windows ครับ เราดูโจทย์กันดีกว่าครับ

input /home/user/images/xxx.jpg
config /home/user => c:\user
output c:\user\images\xxx.jpg

โดยเจ้าโปรแกรมที่เขียนต้องสามารถ maintain และ reuse ได้โดยเจ้าโปรแกรมที่เขียนได้ประมาณนี้ครับ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package softwareskill; 
 
/**
 *
 * @author mLri
 */
public final class PathReplace {
 
String windowsPath;
    String linuxPath;
 
public static void main(String[] args) {
        PathReplace path = new PathReplace("c:\\thai", "/home/new");
        String testPath = path.parsePath("/home/new/images/xxx.jpg");
        System.out.println("testPath is "+testPath);
    }
 
public PathReplace(String pathConfig, String pathConvertTo) {
        windowsPath = pathConfig;
        linuxPath = pathConvertTo;
    }
 
public String parsePath(String path) {
        String newPath = path.replace(linuxPath, windowsPath);
        String convertWindowsPath = newPath.replace("/","\\");
        //System.out.println("newPath is " + newPath);
        //System.out.println("windowsPath is "+windowsPath);
        return convertWindowsPath;
    }
}

Related posts:

  1. Set Path On Ubuntu ...
  2. Java Communications API on Windows ผมเปลี่ยนโปรแจคมาทำเรื่องเกี่ยวกะ การอ่าน RFID โดยการติดต่อจะเขียนโปรแกรมโดยใช้ Java จึงจำเป็นต้องรู้เรื่องของการอ่านข้อมูลมากจาก RS232 หรือว่า Serial Port...

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply