Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 逐行写入文件_Java_Java Io - Fatal编程技术网

Java 逐行写入文件

Java 逐行写入文件,java,java-io,Java,Java Io,我正在从文本[utf8格式]文件中读取数据,并将其存储在字符串中,然后将该字符串转换为十六进制格式,并将该十六进制字符串保存到另一个文件中,但我想逐行保存转换后的十六进制字符串。怎么做 String sCurrentLine; br = new BufferedReader(new FileReader("demmo123.txt")); while ((sCurrentLine = br.readLine()) != null) {

我正在从文本[utf8格式]文件中读取数据,并将其存储在字符串中,然后将该字符串转换为十六进制格式,并将该十六进制字符串保存到另一个文件中,但我想逐行保存转换后的十六进制字符串。怎么做

        String sCurrentLine;
        br = new BufferedReader(new FileReader("demmo123.txt"));
        while ((sCurrentLine = br.readLine()) != null) {
            sb1.append(sCurrentLine);
        }
        String ss = sb1.toString();
        Scanner sc = new Scanner(ss);
        String helloWorldHex = toHexString(ss.getBytes());
        file = new File("demmo.txt");
        fop = new FileOutputStream(file);
        if (!file.exists()) {
            file.createNewFile();
        }
        byte[] contentInBytes = helloWorldHex.getBytes();
        fop.write(helloWorldHex.getBytes());
        fop.write(contentInBytes);
        fop.flush();
        fop.close();
        fop.write(helloWorldHex.getBytes());
对于输入数据:

test string 1
test string 2
test string 3
test string 4


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Example {
    public static void main(String[] args) {

        try {
            File input = new File("C:\\temp\\input.txt");
            File output = new File("C:\\temp\\output.txt");

            Scanner scanner = new Scanner(input);
            PrintWriter printer = new PrintWriter(output);
            while(scanner.hasNextLine()) {
                String string = scanner.nextLine();
                StringBuilder stringBuilder = new StringBuilder(200);
                for(char ch: string.toCharArray()) {
                    if(stringBuilder.length() > 0) stringBuilder.append(' ');
                    stringBuilder.append(String.format("%04x", (int)ch));
                }
                printer.write(stringBuilder.toString());                    
                printer.write("\r\n");
            }
            printer.flush();
            printer.close();
        }
        catch(FileNotFoundException e) {
            System.err.println("File not found.");
        }
    }

}
它给出:

0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0031
0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0032
0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0033
0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0034

显示到目前为止的代码。字符串sCurrentLine;br=新的BufferedReader(新的文件读取器(“demmo123.txt”);while((sCurrentLine=br.readLine())!=null){sb1.append(sCurrentLine);}String ss=sb1.toString();扫描仪sc=新扫描仪(ss);字符串helloWorldHex=toHexString(ss.getBytes());file=新文件(“demmo.txt”);fop=新文件输出流(文件);如果(!file.exists()){file.createNewFile();}字节[]contentInBytes=helloWorldHex.getBytes();write(helloWorldHex.getBytes());//fop.write(contentInBytes);fop.flush();fop.close();write(helloWorldHex.getBytes())@用户3134139:请将您的代码添加到您的问题中。使用
edit
链接。@user3134139不在注释中发布代码(几乎不可能阅读,更不用说如果您在注释中有
/…
注释,只将其复制粘贴到IDE并不容易)。为了达到这个目的,你需要学习文件io。这是java文档
FileWriter writer = new FileWriter("outputfile.txt"); // use your file extension
String content = "My first line";
writer.write(content+"\n");
writer.flush();
//if you have array of string use for loop 
writer.close();
// you should use exception handing always and import relevant classes