关于java导出txt的信息

博主:adminadmin 2023-03-17 01:27:08 548

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

本文目录一览:

java 数据输出到txt文件

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;

public class TestBaiduKnow {

public static void main(String[] args) throws IOException {

FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));

PrintStream p = new PrintStream(fs);

p.println(100);

p.close();

}

}

//简单的一个例子,来模拟输出

java结果输出至txt

帮你修改了一下 你看看可以吗

package com.isoftstone.baidu;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

import java.util.TreeSet;

public class Article {

    //保存文章的内容

    String content;

    //保存分割后的单词集合

    String[] rawWords;

    //保存统计后的单词集合

    String[] words;

    //保存单词对应的词频

    int[] wordFreqs;

    //构造函数,输入文章内容

    //提高部分:从文件中读取

    public Article() {

        content =

                "kolya is one of the richest films i've seen in some time . zdenek sverak plays a confirmed old bachelor ( who's likely to remain so ) , who finds his life as a czech cellist increasingly impacted by the five-year old boy that he's taking care of . though it ends rather abruptly-- and i'm whining , 'cause i wanted to spend more time with these characters-- the acting , writing , and production values are as high as , if not higher than , comparable american dramas . this father-and-son delight-- sverak also wrote the script , while his son , jan , directed-- won a golden globe for best foreign language film and , a couple days after i saw it , walked away an oscar . in czech and russian , with english subtitles . ";

    }

    //对文章根据分隔符进行分词,将结果保存到rawWords数组中

    public void splitWord() {

        //分词的时候,因为标点符号不参与,所以所有的符号全部替换为空格

        final char SPACE = ' ';

        content = content.replace('\'', SPACE).replace(',', SPACE).replace('.', SPACE);

        content = content.replace('(', SPACE).replace(')', SPACE).replace('-', SPACE);

        rawWords = content.split("\\s+");//凡是空格隔开的都算单词,上面替换了', 所以I've 被分成2个 //单词

    }

    //统计词,遍历数组

    public void countWordFreq() {

        //将所有出现的字符串放入唯一的set中,不用map,是因为map寻找效率太低了

        SetString set = new TreeSetString();

        for (String word : rawWords) {

            set.add(word);

        }

        Iterator ite = set.iterator();

        ListString wordsList = new ArrayListString();

        ListInteger freqList = new ArrayListInteger();

        //多少个字符串未知,所以用list来保存先

        while (ite.hasNext()) {

            String word = (String) ite.next();

            int count = 0;//统计相同字符串的个数

            for (String str : rawWords) {

                if (str.equals(word)) {

                    count++;

                }

            }

            wordsList.add(word);

            freqList.add(count++);

        }

        //存入数组当中

        words = wordsList.toArray(new String[0]);

        wordFreqs = new int[freqList.size()];

        for (int i = 0; i freqList.size(); i++) {

            wordFreqs[i] = freqList.get(i);

        }

    }

    //根据词频,将词数组和词频数组进行降序排序

    public void sort() {

        class Word {

            private String word;

            private int freq;

            public Word(String word, int freq) {

                this.word = word;

                this.freq = freq;

            }

        }

        //注意:此处排序,1)首先按照词频降序排列, 2)如果词频相同,按照字母降序排列,

        //如 'abc' 'ab' 'aa'

        class WordComparator implements Comparator {

            public int compare(Object o1, Object o2) {

                Word word1 = (Word) o1;

                Word word2 = (Word) o2;

                if (word1.freq word2.freq) {

                    return 1;

                } else if (word1.freq word2.freq) {

                    return -1;

                } else {

                    int len1 = word1.word.trim().length();

                    int len2 = word2.word.trim().length();

                    String min = len1 len2 ? word2.word : word1.word;

                    String max = len1 len2 ? word1.word : word2.word;

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

                        if (min.charAt(i) max.charAt(i)) {

                            return 1;

                        }

                    }

                    return 1;

                }

            }

        }

        List wordList = new ArrayListWord();

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

            wordList.add(new Word(words[i], wordFreqs[i]));

        }

        Collections.sort(wordList, new WordComparator());

        for (int i = 0; i wordList.size(); i++) {

            Word wor = (Word) wordList.get(i);

            words[i] = wor.word;

            wordFreqs[i] = wor.freq;

        }

    }

    //将排序结果输出

    public void printResult() {

        System.out.println("Total " + words.length + " different words in the content!");

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

            System.out.println(wordFreqs[i] + "  " + words[i]);

        }

    }

    // 输出到文本

    private void outputResult() {

        File file = new File("C:\\output.txt");

        try {

            if (file.exists()) file.delete();

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

            StringBuffer out = new StringBuffer();

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

                out.append(wordFreqs[i] + "  " + words[i] + "\n");

            }

            bw.write(out.toString());

            bw.flush();

            bw.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    //测试类的功能

    public static void main(String[] args) {

        Article a = new Article();

        a.splitWord();

        a.countWordFreq();

        a.sort();

        a.printResult();

        a.outputResult();

    }

}

如何用JAVA生成TXT文件

生成TXT的方法有很多的。常用位字节流和字符流

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

public class TextFileGenerator {

public static void main(String[] args) throws Exception {

method1();

method2();

}

private static void method1() throws Exception {

String txtContent = "Hello World!";

File file = new File("test1.txt");

FileWriter fw = new FileWriter(file);

fw.write(txtContent);

fw.close();

}

private static void method2() throws Exception {

String txtContent = "Hello World!";

File file = new File("test2.txt");

FileOutputStream fos = new FileOutputStream(file);

fos.write(txtContent.getBytes());

fos.close();

}

}

java导出txt文件的问题?

我觉的你的问题在于在循环中一直调用response.getWriter().print();这句,计算机运行中response.getWriter()会不停的生成一个PrintWriter类的对象,导致堆空间在短时间内生成大量的对象,在垃圾回收器未来的及回收之前就内存溢出了。

建议修改:在循环外使用PrintWriter pw=response.getWriter();

循环内使用pw.print();方法。再试试看

如果你的list里放了太多的数据,这样自身就会内存溢出。list中的对象如果没内存溢出,再使用上面说的方法试,不要再用StringBuffer存list中的数据,StringBuffer存list内数据时也是占用内存的,这样你内存消耗的更快。

list最好分成多次存储对象。

如何用java输出txt文件

输入无需使用字节流,直接字符流读取即可。

private void input(String fileName) throws IOException {

    try(BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

        String line;

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

            System.out.println(line);

        }   

    }

}

同样输出,只要把Input换成Output;

private void output(String fileName, String content) throws IOException{

    try(BufferedWriter  writer = new BufferedWriter(new FileWriter(fileName))) {

        writer.write(content);

        writer.flush();

    }

}

java导出txt的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java导出txt的信息别忘了在本站进行查找喔。