Java读取文件内容并输出(JAVA编程:读文件,按行输出文件内容)

2023-11-28 09:00:03 :31

java读取文件内容并输出(JAVA编程:读文件,按行输出文件内容)

大家好,如果您还对java读取文件内容并输出不太了解,没有关系,今天就由本站为大家分享java读取文件内容并输出的知识,包括JAVA编程:读文件,按行输出文件内容的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!

本文目录

JAVA编程:读文件,按行输出文件内容

其实你贴的代码并没有问题

不过你可能也发现了,出现了乱码。当然这个乱码不是必然产生的。

这段代码或使用当前环境默认的编码方式去读取test.txt的字符串,如果默认编码与test.txt的编码不一致就可能会导致乱码。

这里附上另一段代码,自定义编码方式

public static void main(String args) {  try {   // 将D:/test.txt文件读取到输入流中   InputStream input = new FileInputStream("D:/test.txt");   // 创建BufferedReader,以gb2312的编码方式读取文件   BufferedReader reader = new BufferedReader(new InputStreamReader(input, "gb2312"));   String line = null;   // 按行读取文本,直到末尾(一般都这么写)   while ((line = reader.readLine()) != null) {    // 打印当前行字符串    System.out.println(line);   }  } catch (FileNotFoundException e) {   e.printStackTrace();  } catch (IOException e) {   e.printStackTrace();  } }

java读取文件内容并计算后格式输出!

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;public class TestFile { private static File file = new File("student"); /** * 从文件student读入学生成绩,文件格式为 * * 000 XXX 100 100 100 * */ public static void read() throws Exception { List《Student》 stus = new ArrayList《Student》(); // 读文件操作 BufferedReader fr = new BufferedReader(new FileReader(file)); String line = null; // 读取每一行,如果结束了,line会为空 while ((line = fr.readLine()) != null && line.trim().length() 》 0) { // 每一行创建一个Student对象,并存入数组中 stus.add(new Student(line)); } fr.close(); // 排序这个List Collections.sort(stus); // 依次打印每个学生 for (Student stu : stus) { System.out.println(stu); } } /** * * 赠送的方法 * * 随机生成文件,length代表要生成几个学生 * * lenth必须小于1000,未做判断 * * @param length */ public static void randomFile(int length) throws IOException { if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file); StringBuffer sb = new StringBuffer(); Random rd = new Random(); for (int i = 0; i 《 length; i++) { // 清空StringBuffer sb.delete(0, sb.length()); if (i 《 10) { sb.append("00"); } else if (i 《 100) { sb.append("0"); } sb.append(i + " "); sb.append("name" + i + " "); sb.append(rd.nextInt(100) + " "); sb.append(rd.nextInt(100) + " "); sb.append(rd.nextInt(100) + "\r\n"); fw.write(sb.toString()); } fw.close(); } public static void main(String args) throws Exception { read(); } /** * 定义一个学生类,简单定义一个成员为String数组,依次代表 * * 学号 姓名 课程1 课程2 课程3 * * 实现Comparable接口,是为了排序所用 * */ private static class Student implements Comparable《Student》 { private String str; public Student(String line) { // 这里应该进行错误判断 // 比如成绩是否是整型等 str = line.trim().split(" "); } // 获取学生平均成绩 public double getAve() { double c1 = Double.parseDouble(str); double c2 = Double.parseDouble(str); double c3 = Double.parseDouble(str); return (c1 + c2 + c3) / 3; } // 实现该方法,如果小于返回-1,大于返回1,相等返回0 public int compareTo(Student o) { if (this.getAve() 》 o.getAve()) { return 1; } else if (this.getAve() 《 o.getAve()) { return -1; } else { return 0; } } // 重写toString方法,用于打印 public String toString() { return str + "\t" + this.getAve(); } }}

java 如何读取文档中的内容 并打印

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;public class Brdemo { public static void main(String args) throws IOException { FileInputStream fis = new FileInputStream( "src/day03/BrDemo.java");//要读的文件路径 InputStreamReader isr = new InputStreamReader(fis);//字符流 BufferedReader br = new BufferedReader(isr); //缓冲 String line = null; while((line=br.readLine())!=null){//字符不等于空 System.out.println(line);//一行一行地输出 } br.close();//关闭文件 }}

java输入文件名,输出该文件的内容

  java读取文件的内容,使用BufferedReader来读取,通过Scanner对象获取输入的文件名,来读取这个文件的内容并输出,具体示例代码如下:

public class txttest {    /**     * 读取txt文件的内容     * @param file 想要读取的文件对象     * @return 返回文件内容     */    public static String txt2String(File file){        String result = "";        try{            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件            String s = null;            while((s = br.readLine())!=null){//使用readLine方法,一次读一行                result = result + "\n" +s;            }            br.close();            }catch(Exception e){            e.printStackTrace();        }        return result;    }        public static void main(String args){        Scanner scan = new Scanner(System.in);        System.out.println("请输入文件名:");        String str = scan.next();        String mulu = "C:\\Users\\hp\\Desktop\\" + str + ".txt";//文件具体目录        File file = new File(mulu);        System.out.println(txt2String(file));    }}

如何使用Java文件流将“E:\\kj.txt”文件中的内容读取并输出到控制台

把文件位置替换成你的就可以了

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class HelloWorld  {

public static void main(String args) throws IOException {

FileInputStream fs = new FileInputStream("E:\\22.txt");

byte;

int k = fs.read(buf, 0, buf.length);

System.out.println(new String(buf))

}

}

JAVA 读取你磁盘上任意一个文本文件,并输出内容

public static void main(String args) { try { String encoding="GBK"; File file=new File("E:\\123.txt"); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ System.out.println(lineTxt); } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } }

请教,怎么用JAVA来读取二进制文件并输出文件内容

Java读取二进制文件,以字节为单位进行读取,还可读取图片、音乐文件、视频文件等,在Java中,提供了四种类来对文件进行操作,分别是InputStream OutputStream Reader Writer ,前两种是对字节流的操作,后两种则是对字符流的操作。示例代码如下:public static void read()FileByBytes(String fileName){ File file = new File(fileName); InputStream in = null; try { System.out.println("一次读一个"); // 一次读一个字节 in = new FileInputStream(file); int tempbyte; while ((tempbyte = in.read()) != -1) { System.out.write(tempbyte); } in.close(); } catch (IOException e) { e.printStackTrace(); return; }

用java写一个方法实现读取一个.java文件里的内容并打印到控制台,打印出来的内容必须和排版的格式一样

import java.io.*; public class ShowFile { public static void main(String args){ String infname="ShowFile.java"; //默认的输入文件名 try{ File fin=new File(infname); //转入的文件对象 BufferedReader in = new BufferedReader(new FileReader(fin)); //打开输入流 String s; while((s = in.readLine()) != null){//读字符串 System.out.println(s); //写出 } in.close(); //关闭缓冲读入流及文件读入流的连接 }catch (FileNotFoundException e1){ //异常处理 e1.printStackTrace(); }catch(IOException e2){ e2.printStackTrace(); } } }

如何用java从txt读取文本,并处理后输出新的文本

public static void main(String args) throws IOException,ClassNotFoundException {try {FileInputStream in = new FileInputStream(//创建输入流"E:\\0326java\\javase\\src\\io\\TestFileInputStream.java");FileOutputStream out = new FileOutputStream("E:\\1.txt");//创建输出流int i = 0;while ((i = in.read()) != -1) {System.out.print((char) i);//循环读取文件数据out.write(i);//每读一个数据输入到1.txt中}out.flush();out.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

java中读入和输出文本文件

  1. 写文件

    import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;public class WriteFile {    public static void main(String args) {        File file = new File("F:" + File.separator + "abcd.txt");        try {            // 注意,这个地方,那个true的参数,代表如果这个文件已经存在了,就把新的内容添加到该文件的最后            // 如果你想重新创建新文件,把true改成false就好了            Writer writer = new OutputStreamWriter(new FileOutputStream(file, true), "GBK");            StringBuilder builder = new StringBuilder();            for (int i = 0; i 《 100; i++) {                int temp = (int) ((Math.random() + 1) * 1000);                builder.append(String.valueOf(temp));                builder.append("|");                temp = (int) ((Math.random() + 1) * 1000);                builder.append(String.valueOf(temp)).append("\n");            }            writer.write(builder.toString());            writer.close();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

  2. 读文件

    import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;public class ReadFile {    public static void main(String args) {        File file = new File("F:" + File.separator + "abcd.txt");        String s = "";        try {            InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");            BufferedReader reader = new BufferedReader(read);            String line;            while ((line = reader.readLine()) != null) {                s += line + "\n";            }            reader.close();            read.close();        } catch (Exception e) {            e.printStackTrace();        }        System.out.println(s);    }}数据的格式:

关于本次java读取文件内容并输出和JAVA编程:读文件,按行输出文件内容的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。

java读取文件内容并输出(JAVA编程:读文件,按行输出文件内容)

本文编辑:admin
Copyright © 2022 All Rights Reserved 威海上格软件有限公司 版权所有

鲁ICP备20007704号

Thanks for visiting my site.