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

📄 test1.java

📁 用java来进行简单的文件的读取写入的操作
💻 JAVA
字号:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import kmeans.ProtoCluster;


/**
 * 
 * @author chenjie
 *
 */
public class Test1 {
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		Test1 t = new Test1();
		String[] s=new String[10];
		for(int i=0;i<10;i++)
			s[i]=Integer.toString(i);
	    t.WriteMyFile(s,"mydata.txt");
	    t.readMyFile("mydata.txt");
	 
		} 
		
	/**
	 * 
	 * @param s
	 * @param filePath
	 */
	public  void WriteMyFile(String[] s,String filePath) { 
		try { 
			FileWriter fw = new FileWriter(filePath);
		    PrintWriter out = new PrintWriter(fw);    
		    for(int i=0;i<s.length;i++)
		    	out.println(s[i]);   
            out.close();
		    fw.close(); 
		    } catch (IOException e) { 
		    	System.out.println("Uh oh, got an IOException error!");
		        e.printStackTrace();
		     }
		} 
	  
	/**
	 * 
	 * @param filePath
	 */
	public  void readMyFile(String filePath) { 
		String record = null;
		int recCount = 0; 
		try { 
			FileReader fr = new FileReader(filePath);
		    BufferedReader br = new BufferedReader(fr);
		    record = new String();
		    while ((record = br.readLine()) != null) {
		    	recCount++;
		        System.out.println(recCount + ": " + record); 
		        }
		    br.close();
		    fr.close(); 
		    } catch (IOException e) { 
		    	System.out.println("Uh oh, got an IOException error!");
		        e.printStackTrace();
		        }
		    } 
	}

⌨️ 快捷键说明

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