如何从存储文本行和ArrayList到一个txt文件? [英] How to store text lines from and arraylist into a .txt file?

查看:109
本文介绍了如何从存储文本行和ArrayList到一个txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经跑进我的程序有问题。波纹管的方法是假设存储在存储ArrayList中的文本行到一个文本文件,我的程序的一部分(例如store.txt)我究竟做错了什么?所述程序编译,但它不文本行存储到所述文件。贝娄是假设存储文本行所述方法

  //这部分门店的字符串转换成文件
静态无效storeTextLinesToFile(列表<串GT; listOfTextLines,字符串文件名){
    尝试{
        PrintWriter的OUTPUTFILE =的新PrintWriter(新的FileWriter(
                C:/Users/Asus/Desktop/zing/store.txt/+文件名));        对(串线:listOfTextLines){
            outputFile.println(线);
        }        outputFile.close();    }赶上(IOException异常IOException异常){
        的System.out.println(\\ n \\ n不能写入文件\\+文件名+\\);
    }
}


解决方案

试试这个。它可运行code。它显示了code你有上述工作正常。我只是改变了文件路径。我从NetBeans中运行,所以文件中的项目的根文件夹结束。

  ProjectRoot
         SRC
         建立
         store.txt


 进口java.io.FileWriter;
进口java.io.IOException异常;
进口的java.io.PrintWriter;
进口的java.util.ArrayList;
进口的java.util.List;公共类StoreToFIle {    公共静态无效的主要(字串[] args){
        清单<串GT;名单=新的ArrayList<>();        的for(int i = 0; I< 20;我++){
            list.add(将String.valueOf(I));
        }        storeTextLinesToFile(名单,store.txt);
    }    静态无效storeTextLinesToFile(列表<串GT; listOfTextLines,字符串文件名){
        尝试{
            PrintWriter的OUTPUTFILE =的新PrintWriter(新的FileWriter(文件名));            对(串线:listOfTextLines){
                outputFile.println(线);
            }            outputFile.close();        }赶上(IOException异常IOException异常){
            的System.out.println(\\ n \\ n不能写入文件\\+文件名+\\);
        }
    }
}

I have ran into a problem with my program. The bellow method is the part of my program that is suppose to store the text lines stored in ArrayList into a text file (e.g store.txt) what am I doing wrong here? The program compiles but it does not store the text lines in to the said file. Bellow is the said method that is suppose to store the text lines

// this part stores the string into a file
static void storeTextLinesToFile(List<String> listOfTextLines, String fileName) {
    try {
        PrintWriter outputFile = new PrintWriter(new FileWriter(
                "C:/Users/Asus/Desktop/zing/store.txt/" + fileName));

        for (String line : listOfTextLines) {
            outputFile.println(line);
        }

        outputFile.close();

    } catch (IOException ioException) {
        System.out.println("\n\n Cannot write to file \"" + fileName + "\"");
    }
}

解决方案

Try this. It's runnable code. It shows the code you have above works fine. I just changed the file path. I'm running from NetBeans so the file ends up in the Project Root folder.

ProjectRoot
         src
         build
         store.txt


import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class StoreToFIle {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            list.add(String.valueOf(i));
        }

        storeTextLinesToFile(list, "store.txt");
    }

    static void storeTextLinesToFile(List<String> listOfTextLines, String fileName) {
        try {
            PrintWriter outputFile = new PrintWriter(new FileWriter(fileName));

            for (String line : listOfTextLines) {
                outputFile.println(line);
            }

            outputFile.close();

        } catch (IOException ioException) {
            System.out.println("\n\n Cannot write to file \"" + fileName + "\"");
        }
    }
}

这篇关于如何从存储文本行和ArrayList到一个txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆