⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileiohelper.java

📁 We intend to develop a wifi enabled p2p file sharing system on a linux platform using jxta and java.
💻 JAVA
字号:
package edu.uiuc.cs.cs327.linuxwifi.services;
import java.awt.*;
import java.io.*;


  /**

    * Class that help in reading and writing files

    */





public class FileIOHelper {



  private static final boolean DEBUG = true;
  private String fileBuf=null;

  /**

    * Reads contents of a file

    * @param sFilepath name/path of the file to read

	* fileBuf member variable contain the contents of file after read

    * @return true if successful

    */
	public boolean FileRead(String sFilepath)
	{
		DebugOut("Trying to read from file: " + sFilepath);
		boolean bSuccess = false;
		FileInputStream fin;
		int i;
		fileBuf = "";
		try
		{
			fin = new FileInputStream(sFilepath);
			do
			{
				i = fin.read();
				if (i != -1)
					fileBuf += (char) i;
			}
			while (i != -1);
			fin.close();
			DebugOut("Read: " + fileBuf);
			bSuccess = true;
			return bSuccess;
		}
		catch (FileNotFoundException exc1)
		{
			DebugOut("File Not Found: " + sFilepath);
			return bSuccess;
		}
		catch (IOException exc2)
		{
			DebugOut("Unable to read from file: " + sFilepath);
			return bSuccess;
		}
	}


  /**

    * Write contents of sBuf to a a file

    * @param sFilepath name/path of the file to write to
    * @param sBuf buffer to write to the file

    * @return true if successful

    */
	public boolean FileWrite(String sFilepath, String sBuf)
	{

		DebugOut("Trying to write " + sBuf + " to file: " + sFilepath);
		boolean bSuccess = false;
		FileOutputStream fout;
		int i = 0;
		int c = 0;
		try
		{
			fout = new FileOutputStream(sFilepath);
			// loop through all characters of sBuf and write them
			// on at a time to the file

			for (i=0; i<sBuf.length(); i++)	{
				c = sBuf.charAt(i);
				fout.write(c);
			}

			fout.close();
			DebugOut("Wrote: " + sBuf);
			bSuccess = true;
			return bSuccess;
		}
		catch (FileNotFoundException exc1)
		{
			DebugOut("File Not Found: " + sFilepath);
			return bSuccess;
		}
		catch (IOException exc2)
		{
			DebugOut("Unable to write to file: " + sFilepath);
			return bSuccess;
		}
	}

	public String getFileBuf() {
		  return(fileBuf);

	}

	private void DebugOut(String sStr)
	{
		if (DEBUG)
			System.out.println(sStr);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -