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

📄 readconfig.java

📁 Java+Eclipse+Mysql的局域网聊天程序源代码
💻 JAVA
字号:
import java.io.*;
import javax.swing.*;
public class ReadConfig
{
	private RandomAccessFile f=null;
	private long filestart=0;
	public ReadConfig()
	{
		////////////////////路径不能包含中文/////////////////
		try
		{
		f=new RandomAccessFile(getClass().getResource("/config.ini").getFile(),"rw");
		filestart=f.getFilePointer();
		}
		catch(IOException e){System.out.println("读config.ini时发生错误");}
	}
	public String getString(String s,boolean decode)
	{
		try
		{
			f.seek(filestart);
			String tmp=f.readLine();
			while(tmp!=null)
			{
				if(tmp.substring(0, 6).equals("["+s+"]"))
				{
					if(decode)		//如果需要解密
						return Encrypt.decode(tmp.substring(6, tmp.length()));
					return tmp.substring(6, tmp.length());
				}
				tmp=f.readLine();
			}
		}
		catch(IOException e)
		{
			System.out.println("--------------读取字段:"+s+" 时发生错误-------------");
			e.printStackTrace();
		}
		return null;
	}
	public void SaveConfig(String type,String s,boolean encode)
	{
		try
		{
			f.seek(0);
			String tmp=f.readLine();
			String writestr="";
			boolean subpoint=false;

			while(tmp!=null)
			{
				if(tmp.length()<=0)
					break;
				if(tmp.substring(0, 6).equals("["+type+"]"))
				{
					subpoint=true;
				}
				if(!subpoint)
				{
					if(writestr.length()>6)
						writestr=writestr+"\n"+tmp;
					else 
						writestr=writestr+tmp;
				}
				else
				{
					if(writestr.length()>6)
					{
						writestr=writestr+"\n["+type+"]"+s;
						subpoint=false;
					}
					else
					{
						writestr="["+type+"]"+s;
						subpoint=false;
					}
				}
				tmp=null;
				tmp=f.readLine();
			}
			f.setLength(0);
			f.seek(0);
			f.writeBytes(writestr);
		}
		catch(IOException e)
		{
			System.out.println("--------------保存配置文件时发生错误-------------");
			e.printStackTrace();
		}
	}
	public void closeRC()
	{
		try
		{
		f.close();
		}
		catch(IOException ioe)
		{
			System.out.println("--------------关闭配置文件时发生错误-------------");
			ioe.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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