java链接txt的简单介绍

博主:adminadmin 2023-03-20 00:28:11 245

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

本文目录一览:

怎样使用java将其输入到txt文件中

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintStream;

import java.io.PrintWriter;

import java.io.RandomAccessFile;

public class WriteStringToTxt {

    public void WriteStringToFile(String filePath) {

        try {

            File file = new File(filePath);

            PrintStream ps = new PrintStream(new FileOutputStream(file));

            ps.println("");// 往文件里写入字符串

            ps.append("");// 在已有的基础上添加字符串

        } catch (FileNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    public void WriteStringToFile2(String filePath) {

        try {

            FileWriter fw = new FileWriter(filePath, true);

            BufferedWriter bw = new BufferedWriter(fw);

            bw.append("在已有的基础上添加字符串");

            bw.write("abc\r\n ");// 往已有的文件上添加字符串

            bw.write("def\r\n ");

            bw.write("hijk ");

            bw.close();

            fw.close();

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    public void WriteStringToFile3(String filePath) {

        try {

            PrintWriter pw = new PrintWriter(new FileWriter(filePath));

            pw.println("abc ");

            pw.println("def ");

            pw.println("hef ");

            pw.close();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    public void WriteStringToFile4(String filePath) {

        try {

            RandomAccessFile rf = new RandomAccessFile(filePath, "rw");

            rf.writeBytes("op\r\n");

            rf.writeBytes("app\r\n");

            rf.writeBytes("hijklllll");

            rf.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    public void WriteStringToFile5(String filePath) {

        try {

            FileOutputStream fos = new FileOutputStream(filePath);

            String s = "";

            fos.write(s.getBytes());

            fos.close();

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    public static void main(String[] args) {

        String filePath = "E:\\link.txt";

        // new WriteStringToTxt().WriteStringToFile(filePath);

        // new WriteStringToTxt().WriteStringToFile2(filePath);

        // new WriteStringToTxt().WriteStringToFile3(filePath);

        // new WriteStringToTxt().WriteStringToFile4(filePath);

        new WriteStringToTxt().WriteStringToFile5(filePath);

    }

}

如何用java读取txt文件

你生成了txt文档,必须要将后缀改为.java,然后使用命令行格式的javac命令进行编译,编译后就会出现.class文件,再用命令行格式的java命令进行运行!

java中如何调用txt里的数据?

要用输入流。

import java.io.*;

void readTxt(String txtpath){

File file = new File(txtPath); // 根据tx立文件路径,建了文件 D:\JAVA\OK.TXT

FileReader fr = null;

bufferedReader br = null;

String str = null;

try{

fr = new FileReader(file); //文件输入流

br = new BufferedReader(fr); // 缓冲输入流

while((str = br.readLine())!=null){ //按行读取txt文件,存在str中

// 可以再这里对str进行操作

}

}catch(IOException e){

System.out.println(e.getMessage());}

try{

fr.close();

br.close();

}catch(IOException e){

System.out.println(e.getMessage());}

}

java代码 如何向TXT文件写入内容?

  向txt文件写入内容基本思路就是获得一个file对象,新建一个txt文件,打开I/O操作流,使用写入方法进行读写内容,示例如下:

package common;

import java.io.*;

import java.util.ArrayList;

public class IOTest {

public static void main (String args[]) {

ReadDate();

WriteDate();

}

/**

* 读取数据

*/

public static void ReadDate() {

String url = “e:/2.txt”;

try {

FileReader read = new FileReader(new File(url));

StringBuffer sb = new StringBuffer();

char ch[] = new char[1024];

int d = read.read(ch);

while(d!=-1){

String str = new String(ch,0,d);

sb.append(str);

d = read.read(ch);

}

System.out.print(sb.toString());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 写入数据

*/

public static void WriteDate() {

try{

File file = new File(“D:/abc.txt”);

if (file.exists()) {

file.delete();

}

file.createNewFile();

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

ArrayList ResolveList = new ArrayList();

for (int i = 0; i  10; i++) {

ResolveList.add(Math.random()* 100);

}

for (int i=0 ;i 

output.write(String.valueOf(ResolveList.get(i)) + “\n”);

}

output.close();

} catch (Exception ex) {

System.out.println(ex);

}

}

}

原文出自【比特网】,转载请保留原文链接:

java客户端如何向服务器txt文件写入信息

客户端要向服务器txt文件写入信息,必须得调用服务器端得java方法,io读写txt文件。客户端用异步调用效果更好,客户端异步传递要写入得信息到服务器上,由服务器上得方法执行写入

求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和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。