<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ไอเอินบล๊อก &#187; write file</title>
	<atom:link href="http://www.iearn.in.th/tag/write-file/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iearn.in.th</link>
	<description>Just Play and Just Knowledge</description>
	<lastBuildDate>Fri, 05 Aug 2011 15:10:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Read File and check path</title>
		<link>http://www.iearn.in.th/2008/05/03/read-file-and-check-path/</link>
		<comments>http://www.iearn.in.th/2008/05/03/read-file-and-check-path/#comments</comments>
		<pubDate>Sat, 03 May 2008 06:24:24 +0000</pubDate>
		<dc:creator>nolifelover</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[BufferedReader]]></category>
		<category><![CDATA[lineBuffer]]></category>
		<category><![CDATA[read file]]></category>
		<category><![CDATA[write file]]></category>

		<guid isPermaLink="false">http://www.jaideawhosting.com/nolifelover/?p=137</guid>
		<description><![CDATA[หลังจากหายหน้าหายตาไปหลายวันไม่ได้ไปไหนนะครับแต่ว่าช่วงนี้งานส่วนของ Asterisk ที่เน้น ruby เป็นหลังต้องทำให้ค้องแบ่งเวลาในการเขียนบล๊อกทั้งหมดไปทำในส่วนนี้สำหรับวันที่วันเสาร์เรียน scjp เสร็จเลยมีอารมณ์อยากที่จะเขียนบล๊อก่อจากวันก่อนครับ สำหรับวันนี้จะเขียนเกี่ยวกับโค้ดของ softskill ต่อละกันนะครับ สำหรับวันนี้โจทย์จะเป็นไปในลักษณะให้เราอ่าน ไฟล์จาก input.txt ซึ่งมี path ของ file อยู่และให้อ่านอีกไฟล์คือไฟล์ rules.txt เพื่อทำการตวรจสอบว่าไฟล์นั้นผ่านกฏต่างๆ ที่ได้กำหนดไว้ในไฟล์ rules.txt หรือไม่หากว่าตรงให้เอาเฉพาะไฟล์ที่อณุญาตเท่านั้นครับ เรามาดูกันเลยดีกว่าครับ โดยในที่นี้ผมจะใช้ Pathmanager ที่ได้เขียนไว้ในการจัดการเรื่อง permission ครับ สำหรับไฟล์ใหม่ที่เขียนขึ้นตามนี้ครับ package softwareskill; &#160; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; &#160; /** * * @author mLri */ public [...]]]></description>
			<content:encoded><![CDATA[<p>หลังจากหายหน้าหายตาไปหลายวันไม่ได้ไปไหนนะครับแต่ว่าช่วงนี้งานส่วนของ Asterisk ที่เน้น ruby เป็นหลังต้องทำให้ค้องแบ่งเวลาในการเขียนบล๊อกทั้งหมดไปทำในส่วนนี้สำหรับวันที่วันเสาร์เรียน scjp เสร็จเลยมีอารมณ์อยากที่จะเขียนบล๊อก่อจากวันก่อนครับ สำหรับวันนี้จะเขียนเกี่ยวกับโค้ดของ softskill ต่อละกันนะครับ สำหรับวันนี้โจทย์จะเป็นไปในลักษณะให้เราอ่าน ไฟล์จาก input.txt ซึ่งมี path ของ file อยู่และให้อ่านอีกไฟล์คือไฟล์ rules.txt เพื่อทำการตวรจสอบว่าไฟล์นั้นผ่านกฏต่างๆ ที่ได้กำหนดไว้ในไฟล์ rules.txt หรือไม่หากว่าตรงให้เอาเฉพาะไฟล์ที่อณุญาตเท่านั้นครับ เรามาดูกันเลยดีกว่าครับ โดยในที่นี้ผมจะใช้ Pathmanager ที่ได้เขียนไว้ในการจัดการเรื่อง permission ครับ สำหรับไฟล์ใหม่ที่เขียนขึ้นตามนี้ครับ</p>
<p><span id="more-137"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">softwareskill</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileNotFoundException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 *
 * @author mLri
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SecueManager <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> rule<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> input<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> output<span style="color: #339933;">;</span>
    PathManager pathManager<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span>, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        SecueManager secueManager <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span>  SecueManager<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Users/mLri/rule.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        secueManager.<span style="color: #006633;">processFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Users/mLri/input.txt&quot;</span>, <span style="color: #0000ff;">&quot;/Users/mLri/output.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> SecueManager<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> rule<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span>, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rule</span> <span style="color: #339933;">=</span> rule<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pathManager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PathManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">readRule</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> processFile<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> input,<span style="color: #003399;">String</span> output<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span>, <span style="color: #003399;">IOException</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">input</span> <span style="color: #339933;">=</span> input<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">output</span> <span style="color: #339933;">=</span> output<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pathProcess</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> readRule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span>, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>rule<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">BufferedReader</span> fileBuffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileReader</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> lineBuffer<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lineBuffer <span style="color: #339933;">=</span> fileBuffer.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lineBuffer.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                pathManager.<span style="color: #006633;">addAcceptedRule</span><span style="color: #009900;">&#40;</span>lineBuffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">//System.out.println(&quot;is complete addAcceptRule is &quot; + lineBuffer.substring(1));</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lineBuffer.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                pathManager.<span style="color: #006633;">addDeniedRule</span><span style="color: #009900;">&#40;</span>lineBuffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">//System.out.println(&quot;is complete addDeniedRule is &quot; + lineBuffer.substring(1));</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        fileBuffer.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;readRule is Successful&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> pathProcess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span>, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">BufferedReader</span> fileBuffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileReader</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">ArrayList</span> allowFile <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> lineBuffer<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lineBuffer <span style="color: #339933;">=</span> fileBuffer.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pathManager.<span style="color: #006633;">isAllow</span><span style="color: #009900;">&#40;</span>lineBuffer<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//System.out.println(&quot;add line &quot; + lineBuffer);</span>
                allowFile.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>lineBuffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        fileBuffer.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pathWrite</span><span style="color: #009900;">&#40;</span>allowFile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> pathWrite<span style="color: #009900;">&#40;</span><span style="color: #003399;">ArrayList</span> allowFile<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">FileWriter</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileWriter</span><span style="color: #009900;">&#40;</span>output,<span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> accept<span style="color: #339933;">:</span>allowFile<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            file.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>accept<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        file.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;File output is Suscessful in &quot;</span><span style="color: #339933;">+</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>สำหรับไฟล์ SecueManager จะเป็นตัวที่ทำหน้าที่ในการอ่านไฟล์และเขียนไฟล์ ส่วนไฟล์ที่ทำการ check จริงๆจะเป็นไฟล์ <a href="http://www.jaideawhosting.com/nolifelover/?p=126">PathManager</a> ครับ</p>

<p class="sayac_bilgi">24 views</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iearn.in.th/2008/05/03/read-file-and-check-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

