CORE
HOME > JAVA > J2SE > CORE
2016.04.22 / 17:37

ÅؽºÆ® ÆÄÀÏ »ý¼º ¹× ³»¿ë ¾÷µ¥ÀÌÆ®

Kiri
Ãßõ ¼ö 439


  1.        // ÆÄÀÏ »ý¼º  
  2. private void CreateFile(String FilePath)  
  3. {  
  4.     try  
  5.     {  
  6.         System.out.println(FilePath);  
  7.           
  8.         int nLast = FilePath.lastIndexOf("\\");  
  9.         String strDir = FilePath.substring(0, nLast);  
  10.         String strFile = FilePath.substring(nLast+1, FilePath.length());  
  11.           
  12.         File dirFolder = new File(strDir);  
  13.         dirFolder.mkdirs();  
  14.         File f = new File(dirFolder, strFile);  
  15.         f.createNewFile();  
  16.     }  
  17.     catch (Exception ex)  
  18.     {  
  19.         System.out.println(ex.getMessage());  
  20.     }  
  21. }  
  22.   
  23. // ÆÄÀÏ Å×½ºÆ® Àб⠠
  24. private String ReadFileText(File file)  
  25. {  
  26.     String strText = "";  
  27.     int nBuffer;  
  28.     try   
  29.     {  
  30.         BufferedReader buffRead = new BufferedReader(new FileReader(file));  
  31.         while ((nBuffer = buffRead.read()) != -1)  
  32.         {  
  33.             strText += (char)nBuffer;  
  34.         }  
  35.         buffRead.close();  
  36.     }  
  37.     catch (Exception ex)  
  38.     {  
  39.         System.out.println(ex.getMessage());  
  40.     }  
  41.       
  42.     return strText;  
  43. }  
  44.   
  45. // ÆÄÀÏ ¼öÁ¤  
  46. private void UpdateFile(String FilePath, String Text)  
  47. {  
  48.     try   
  49.     {  
  50.         File f = new File(FilePath);  
  51.         if (f.exists() == false)  
  52.         {  
  53.             // ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì ÆÄÀÏÀ» ¸¸µé¤¤´Ù.  
  54.             CreateFile(FilePath);  
  55.         }  
  56.           
  57.         // ÆÄÀÏ Àб⠠
  58.         String fileText = ReadFileText(f);  
  59.         BufferedWriter buffWrite = new BufferedWriter(new FileWriter(f));  
  60.         Text = fileText + "\r\n" + Text;  
  61.         // ÆÄÀÏ ¾²±â  
  62.         buffWrite.write(Text, 0, Text.length());  
  63.         // ÆÄÀÏ ´Ý±â  
  64.         buffWrite.flush();  
  65.         buffWrite.close();  
  66.     }  
  67.     catch (Exception ex)  
  68.     {  
  69.         System.out.println(ex.getMessage());  
  70.     }  
  71. }