包含java传入txt的词条

博主:adminadmin 2023-01-04 02:36:11 786

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

本文目录一览:

在Java中,我如何将JTextField里输入的内容传进TXT文档中保存下来。

java中用getText()获取textfield内容,然后创建一个save方法保存内容即可。

save方法内容如下:

void saveToFile(String fileName, JTextField textField) throws Exception {

FileOutputStream out = new FileOutputStream(fileName, true);

out.write(textField.getText().getBytes());

}

java 怎么将数据写入TXT文件

定义一个输出文件,然后输出就可以了,具体见下面的代码

 import java.io.*;

 public class StreamDemo

 {

  public static void main(String args[])

  {

   File f = new File("c:\\temp.txt") ;

   OutputStream out = null ;

   try 

   {

    out = new FileOutputStream(f) ;

   } 

   catch (FileNotFoundException e) 

   {

    e.printStackTrace();

   }

   // 将字符串转成字节数组

   byte b[] = "Hello World!!!".getBytes() ;

   try 

   {

    // 将byte数组写入到文件之中

    out.write(b) ;

   } 

   catch (IOException e1) 

   {

    e1.printStackTrace();

   }

   try 

   {

    out.close() ;

   } 

   catch (IOException e2) 

   {

    e2.printStackTrace();

   }

  

   // 以下为读文件操作

   InputStream in = null ;

   try 

   {

    in = new FileInputStream(f) ;

   } 

   catch (FileNotFoundException e3) 

   {

    e3.printStackTrace();

   }

   // 开辟一个空间用于接收文件读进来的数据

   byte b1[] = new byte[1024] ;

   int i = 0 ;

   try 

   {

   // 将b1的引用传递到read()方法之中,同时此方法返回读入数据的个数

    i = in.read(b1) ;

   } 

   catch (IOException e4) 

   {

    e4.printStackTrace();

   }

   try 

   {

    in.close() ;

   } 

   catch (IOException e5) 

   {

    e5.printStackTrace();

   }

   //将byte数组转换为字符串输出

   System.out.println(new String(b1,0,i)) ;

  }

 }

java如何从数据库读取数据并写入txt文件?

写Java程序时经常碰到要读如txt或写入txt文件的情况,但是由于要定义好多变量,经常记不住,每次都要查,特此整理一下,简单易用,方便好懂!

[java] view plain copy

package edu.thu.keyword.test;  

  

import java.io.File;  

import java.io.InputStreamReader;  

import java.io.BufferedReader;  

import java.io.BufferedWriter;  

import java.io.FileInputStream;  

import java.io.FileWriter;  

  

public class cin_txt {  

    static void main(String args[]) {  

        try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw  

  

            /* 读入TXT文件 */  

            String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径  

            File filename = new File(pathname); // 要读取以上路径的input。txt文件  

            InputStreamReader reader = new InputStreamReader(  

                    new FileInputStream(filename)); // 建立一个输入流对象reader  

            BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言  

            String line = "";  

            line = br.readLine();  

            while (line != null) {  

                line = br.readLine(); // 一次读入一行数据  

            }  

  

            /* 写入Txt文件 */  

            File writename = new File(".\\result\\en\\output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件  

            writename.createNewFile(); // 创建新文件  

            BufferedWriter out = new BufferedWriter(new FileWriter(writename));  

            out.write("我会写入文件啦\r\n"); // \r\n即为换行  

            out.flush(); // 把缓存区内容压入文件  

            out.close(); // 最后记得关闭文件  

  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

    }  

}

怎样使用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文件写入内容基本思路就是获得一个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文件里格式是什么样的?

import java.nio.file.*;

import java.io.*;

import java.util.stream.*;

try(var writer = new PrintWriter("/tmp/numbers.txt")){

IntStream.rangeClosed(200, 20000).forEach(writer::println); //生成一个每行一个整数的文本文件

Files.lines(Paths.get("/tmp/numbers.txt")).mapToInt(Integer::parseInt).toArray(); // 将文件中的数字读到一个数组里

} catch(Exception e){

e.printStackTrace();

}

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