Andy 胡

导航

C# & JAVA:读写文件

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication4 {
class Program {
public  static  void Read(string path) {
FileStream  fs = new FileStream(path, FileMode.Open);
StreamReader  read = new  StreamReader(fs, Encoding.Default);
string strReadline;

int nLine = 1;
while ((strReadline = read.ReadLine()) != null)
            {
Console.WriteLine((nLine++) + ":" + strReadline);
            }
            fs.Close();
            read.Close();
        }

static void  Main(string[] args) {
            Read(@"D:\ExamData\西游记.txt");
        }
    }
}

  ↑C#

↓Java

import java.io.*;

public class ReadFile {
	public static void Read(Stringpath) {
		try {
			FileInputStream fis = new FileInputStream(path);
			InputStreamReader isr = new InputStreamReader(fis, "GBK");
			BufferedReader br = new BufferedReader(isr);
			String line = "";
			int nLine = 1;
			while ((line = br.readLine()) != null) {
				System.out.println((nLine++) + ":" + line);
			}
			br.close();
			isr.close();
			fis.close();
		} catch (IOExceptione) {
			e.printStackTrace();
		}
	}

	publicstaticvoid main(String[] args) {
		Read("D:/ExamData/西游记.txt");
	}
}

  

posted on 2018-11-18 15:25  talkwah  阅读(214)  评论(0编辑  收藏  举报