包含java对txt文件操作的词条

博主:adminadmin 2022-11-23 06:33:05 60

今天给各位分享java对txt文件操作的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java读txt方法

1).按行读取TXT文件

package zc;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class readLine {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("C:/zc.txt");

BufferedReader reader = null;

String tempString = null;

int line =1;

try {

System.out.println("以行为单位读取文件内容,一次读一整行:");

reader = new BufferedReader(new FileReader(file));

while ((tempString = reader.readLine()) != null) {

System.out.println("Line"+ line + ":" +tempString);

line ++ ;

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(reader != null){

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

2).按字节读取TXT文件

package zc;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

public class readerFileByChars {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("c:/zc.txt");

InputStream in = null;

byte[] tempByte = new byte[1024];

int byteread = 0;

try {

System.out.println("以字节为单位读取文件内容,一次读多个字节:");

in = new FileInputStream(file);

while ((byteread = in.read(tempByte)) != -1 ) {

System.out.write(tempByte, 0, byteread);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if (in != null) {

try {

in.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

java怎样实现读写TXT文件

主要有用到java原生态的Io类,没有第三个包。直接上代码:

import java.io.*;

public class write {

    public static void main(String[] args) {

        write("E://123.txt", "hello");

    }

    public static void write(String path, String content) {

        try {

            File f = new File(path);

            

            if (f.exists()) {

                System.out.println("文件存在");

            } else {

                System.out.println("文件不存在,正在创建...");

                if (f.createNewFile()) {

                    System.out.println("文件创建成功!");

                } else {

                    System.out.println("文件创建失败!");

                }

            }

            BufferedWriter output = new BufferedWriter(new FileWriter(f));

            output.write(content);

            output.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

求java操作txt文件的方法

/**

 * @date:2015年5月11日 上午9:58:42

 * @Description:读取指定行内容,不包括空行

 * @param n

 * @return

 */

public String _read(int n) throws Exception {

ListString list = new ArrayListString();

BufferedReader b = null;

File file = new File("C:\\Users\\Administrator\\Desktop\\远程调用.txt");

b = new BufferedReader(new FileReader(file));

String line = null;

while ((line = b.readLine()) != null) {

if (!line.equals("")) {

list.add(line);

}

}

b.close();

return list.get(n - 1);

}

/** 

* @date:2015年5月11日 上午11:54:16

* @Description:修改指定行的指定数组内容

* @param n

* @param str

* @throws Exception 

*/ 

public void _change(int n, String[] str) throws Exception {

File file = new File("C:\\Users\\Administrator\\Desktop\\远程调用.txt");

BufferedReader b=new BufferedReader(new FileReader(file));

StringBuffer sb = new StringBuffer();

StringBuffer sb1= new StringBuffer();

for (int i = 0; i  str.length; i++) {

sb.append(str[i]);

}

String line = null;

int index=1;

while ((line = b.readLine()) != null) {

if (!line.equals("")index!=n) {

sb1.append(line);

sb1.append(System.getProperty("line.separator"));

index++;

}else if(!line.equals("")index==n){

sb1.append(sb.toString());

sb1.append(System.getProperty("line.separator"));

index++;

}

}

b.close();

    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    bw.write(sb1.toString());

bw.close();

}

关于java对txt文件操作和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。