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

📄 10c4d15e350b001c1915ec98d6fbfb09

📁 基于linux操作系统
💻
字号:
package deng;

import java.io.*;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class FilesIO {
	
//	public static void main(String a[]){   
//		File file=new File("E:\\FireWall");
//		if(file.exists()){
//			System.out.println("****************");
//			try {
//				file=new File("E:\\FireWall\\RuleScript.txt");
//				FileWriter fw=new FileWriter(file);
//				fw.write("LJGHKJGKJGKJ");
//				fw.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//		}else{
//			try {
//				if(file.mkdirs())
//					System.out.println("?????????????");
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}
	
	
	public static void writeRuleToScript(Rule rule,PrintWriter pw){
		
		String strRule="";
		String[] direction=rule.getDirection();
		
		for(int i=0;i<direction.length;i++){
			if(direction[i]!=null)
			{  //System.out.println(direction[i]+"????????");
				strRule="iptable -A  "+direction[i]+" -p "+rule.getProtocol()+" -s "+rule.getIP()
				  +" --sport "+rule.getSport()+" --dport "+rule.getDport()+" -j "+rule.getStrategy();
				pw.println(strRule);
				//  System.out.println(strRule);
			}
		}
	}
	
	
	//如何文件中天插入信息而不覆盖原有信息?在fileOutputStream的构造函数中假如一个参数true
	public static boolean writeAllRulesToScript(MyArrayList ruleList){
		File file=new File("E:\\FireWall");
		FileOutputStream fos=null;
		PrintWriter pw=null;
		boolean flag=false;
		if(file.exists()){
		try {
			file=new File("E:\\FireWall\\RuleScript");
			fos=new FileOutputStream(file,true);
			pw=new PrintWriter(fos);
			
			for(int i=0;i<ruleList.size();i++){
				Rule rule=(Rule)ruleList.get(i);
				   writeRuleToScript(rule,pw);
			  }
			flag=true;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			 flag=false;
		    JOptionPane.showMessageDialog(null,"向脚本文件中写入时出错!" );	
		}	
		
		}else{
			
			try {
				file.mkdirs();
				file=new File("E:\\FireWall\\RuleScript");
				fos=new FileOutputStream(file,true);
				pw=new PrintWriter(fos);
				
				for(int i=0;i<ruleList.size();i++){
					Rule rule=(Rule)ruleList.get(i);
					   writeRuleToScript(rule,pw);
				  }
				flag=true;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				 flag=false;
			    JOptionPane.showMessageDialog(null,"向脚本文件中写入时出错!" );	
			}	
//			 flag=false;
//			JOptionPane.showMessageDialog(null," 请事先创建文件夹E:\\FireWall!" );
				
		}
		try{
			if(pw!=null)
			pw.close();
		}catch(Exception e){
			e.printStackTrace();
			flag=false;
			JOptionPane.showMessageDialog(null,"无法关闭资源!" );
		}
		
		return flag;
	}
	
	
	
	public static void saveRuleListToFile(MyArrayList ruleList){
		File file=new File("E:\\FireWall");
		FileOutputStream fos=null;
		ObjectOutputStream oos=null;
		if(file.exists()){
			try {
				file=new File("E:\\FireWall\\RuleScript.txt");
				fos=new FileOutputStream(file);
				oos=new ObjectOutputStream(fos);
				oos.writeObject(ruleList);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,"向文件中保存数据时出错!" );
			}
			
		}else{
			try {
				file.mkdirs();
				file=new File("E:\\FireWall\\RuleScript.txt");
				fos=new FileOutputStream(file);
				oos=new ObjectOutputStream(fos);
				oos.writeObject(ruleList);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,"向文件中保存数据时出错!" );
			}
			
			
		}
		
		try{
		   if(oos!=null)oos.close();
		}catch(Exception ee){
			ee.printStackTrace();
			JOptionPane.showMessageDialog(null,"无法关闭资源!" );
		}
	}
	
	
	public static MyArrayList readRuleListFromFile(){
		File file=new File("E:\\FireWall\\RuleScript.txt");
		MyArrayList myArrayList=new MyArrayList();
		FileInputStream fis=null;
		ObjectInputStream ois=null;
		if(file.exists()){
			try {
				fis=new FileInputStream(file);
			    ois=new ObjectInputStream(fis);
				myArrayList=(MyArrayList)ois.readObject();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,"从文件中读取数据时出错!" );
			}
			
		}else{
				
			JOptionPane.showMessageDialog(null,"从文件中读取数据时出错!" );
			
		}
		
		try{
			if(ois!=null)ois.close();
			}catch(Exception ee){
				ee.printStackTrace();
				JOptionPane.showMessageDialog(null,"无法关闭资源!" );
			}
		return myArrayList;
	}

}

⌨️ 快捷键说明

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