HOME 首页
SERVICE 服务产品
XINMEITI 新媒体代运营
CASE 服务案例
NEWS 热点资讯
ABOUT 关于我们
CONTACT 联系我们
创意岭
让品牌有温度、有情感
专注品牌策划15年

    chars怎么读(chars怎么读音)

    发布时间:2023-03-13 12:53:04     稿源: 创意岭    阅读: 76        问大家

    大家好!今天让创意岭的小编来大家介绍下关于chars怎么读的问题,以下是小编对此问题的归纳整理,让我们一起来看看吧。

    ChatGPT国内免费在线使用,一键生成原创文章、方案、文案、工作计划、工作报告、论文、代码、作文、做题和对话答疑等等

    只需要输入关键词,就能返回你想要的内容,越精准,写出的就越详细,有微信小程序端、在线网页版、PC客户端

    官网:https://ai.de1919.com

    本文目录:

    chars怎么读(chars怎么读音)

    一、利用字节文件输入输出流,编写程序完成文件的读,写,复制功能。

    import java.io.BufferedReader;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileReader;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.io.RandomAccessFile;

    import java.io.Reader;

    /**

    * 多种方式读文件内容。 按字节读取文件内容、按字符读取文件内容、按行读取文件内容、随机读取文件内容

    */

    public class ReadFromFile

    {

    public static void main(String[] args)

    {

    String fileName = "C:/temp/newTemp.txt";

    ReadFromFile.readFileByBytes(fileName);

    ReadFromFile.readFileByChars(fileName);

    ReadFromFile.readFileByLines(fileName);

    ReadFromFile.readFileByRandomAccess(fileName);

    }

    /**

    * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

    *

    * @param fileName

    * 文件的名

    */

    public static void readFileByBytes(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;

    }

    try

    {

    System.out.println("以字节为单位读取文件内容,一次读多个字节:");

    // 一次读多个字节

    byte[] tempbytes = new byte[100];

    int byteread = 0;

    in = new FileInputStream(fileName);

    ReadFromFile.showAvailableBytes(in);

    // 读入多个字节到字节数组中,byteread为一次读入的字节数

    while ((byteread = in.read(tempbytes)) != -1)

    {

    System.out.write(tempbytes, 0, byteread);

    }

    }

    catch (Exception e1)

    {

    e1.printStackTrace();

    }

    finally

    {

    if (in != null)

    {

    try

    {

    in.close();

    }

    catch (IOException e1)

    {

    }

    }

    }

    }

    /**

    * 以字符为单位读取文件,常用于读文本,数字等类型的文件

    *

    * @param fileName

    * 文件名

    */

    public static void readFileByChars(String fileName)

    {

    File file = new File(fileName);

    Reader reader = null;

    try

    {

    System.out.println("以字符为单位读取文件内容,一次读一个字节:");

    // 一次读一个字符

    reader = new InputStreamReader(new FileInputStream(file));

    int tempchar;

    while ((tempchar = reader.read()) != -1)

    {

    // 对于windows下,\r\n这两个字符在一起时,表示一个换行。

    // 但如果这两个字符分开显示时,会换两次行。

    // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

    if (((char) tempchar) != '\r')

    {

    System.out.print((char) tempchar);

    }

    }

    reader.close();

    }

    catch (Exception e)

    {

    e.printStackTrace();

    }

    try

    {

    System.out.println("以字符为单位读取文件内容,一次读多个字节:");

    // 一次读多个字符

    char[] tempchars = new char[30];

    int charread = 0;

    reader = new InputStreamReader(new FileInputStream(fileName));

    // 读入多个字符到字符数组中,charread为一次读取字符数

    while ((charread = reader.read(tempchars)) != -1)

    {

    // 同样屏蔽掉\r不显示

    if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r'))

    {

    System.out.print(tempchars);

    }

    else

    {

    for (int i = 0; i < charread; i++)

    {

    if (tempchars[i] == '\r')

    {

    continue;

    }

    else

    {

    System.out.print(tempchars[i]);

    }

    }

    }

    }

    }

    catch (Exception e1)

    {

    e1.printStackTrace();

    }

    finally

    {

    if (reader != null)

    {

    try

    {

    reader.close();

    }

    catch (IOException e1)

    {

    }

    }

    }

    }

    /**

    * 以行为单位读取文件,常用于读面向行的格式化文件

    *

    * @param fileName

    * 文件名

    */

    public static void readFileByLines(String fileName)

    {

    File file = new File(fileName);

    BufferedReader reader = null;

    try

    {

    System.out.println("以行为单位读取文件内容,一次读一整行:");

    reader = new BufferedReader(new FileReader(file));

    String tempString = null;

    int line = 1;

    // 一次读入一行,直到读入null为文件结束

    while ((tempString = reader.readLine()) != null)

    {

    // 显示行号

    System.out.println("line " + line + ": " + tempString);

    line++;

    }

    reader.close();

    }

    catch (IOException e)

    {

    e.printStackTrace();

    }

    finally

    {

    if (reader != null)

    {

    try

    {

    reader.close();

    }

    catch (IOException e1)

    {

    }

    }

    }

    }

    /**

    * 随机读取文件内容

    *

    * @param fileName

    * 文件名

    */

    public static void readFileByRandomAccess(String fileName)

    {

    RandomAccessFile randomFile = null;

    try

    {

    System.out.println("随机读取一段文件内容:");

    // 打开一个随机访问文件流,按只读方式

    randomFile = new RandomAccessFile(fileName, "r");

    // 文件长度,字节数

    long fileLength = randomFile.length();

    // 读文件的起始位置

    int beginIndex = (fileLength > 4) ? 4 : 0;

    // 将读文件的开始位置移到beginIndex位置。

    randomFile.seek(beginIndex);

    byte[] bytes = new byte[10];

    int byteread = 0;

    // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

    // 将一次读取的字节数赋给byteread

    while ((byteread = randomFile.read(bytes)) != -1)

    {

    System.out.write(bytes, 0, byteread);

    }

    }

    catch (IOException e)

    {

    e.printStackTrace();

    }

    finally

    {

    if (randomFile != null)

    {

    try

    {

    randomFile.close();

    }

    catch (IOException e1)

    {

    }

    }

    }

    }

    /**

    * 显示输入流中还剩的字节数

    *

    * @param in

    */

    private static void showAvailableBytes(InputStream in)

    {

    try

    {

    System.out.println("当前字节输入流中的字节数为:" + in.available());

    }

    catch (IOException e)

    {

    e.printStackTrace();

    }

    }

    }

    /////////////////////////////////////////////////////////////////

    package io;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.OutputStream;

    public class CopyFileUtil

    {

    /**

    * 复制整个目录的内容,如果目标目录存在,则不覆盖

    *

    * @param srcDirName

    * 待复制的目录名

    * @param destDirName

    * 目标目录名

    * @return 如果复制成功返回true,否则返回false

    */

    public static boolean copyDirectory(String srcDirName, String destDirName)

    {

    return CopyFileUtil.copyDirectory(srcDirName, destDirName, false);

    }

    /**

    * 复制整个目录的内容

    *

    * @param srcDirName

    * 待复制的目录名

    * @param destDirName

    * 目标目录名

    * @param overlay

    * 如果目标目录存在,是否覆盖

    * @return 如果复制成功返回true,否则返回false

    */

    public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)

    {

    // 判断原目录是否存在

    File srcDir = new File(srcDirName);

    if (!srcDir.exists())

    {

    System.out.println("复制目录失败:原目录" + srcDirName + "不存在!");

    return false;

    }

    else if (!srcDir.isDirectory())

    {

    System.out.println("复制目录失败:" + srcDirName + "不是一个目录!");

    return false;

    }

    // 如果目标文件夹名不以文件分隔符结尾,自动添加文件分隔符

    if (!destDirName.endsWith(File.separator))

    {

    destDirName = destDirName + File.separator;

    }

    File destDir = new File(destDirName);

    // 如果目标文件夹存在,

    if (destDir.exists())

    {

    if (overlay)

    {

    // 允许覆盖则删除已存在的目标目录

    System.out.println("目标目录已存在,准备删除它!");

    if (!DeleteFileUtil.delete(destDirName))

    {

    System.out.println("复制目录失败:删除目标目录" + destDirName + "失败!");

    }

    }

    else

    {

    System.out.println("复制目录失败:目标目录" + destDirName + "已存在!");

    return false;

    }

    }

    else

    {

    // 创建目标目录

    System.out.println("目标目录不存在,准备创建它!");

    if (!destDir.mkdirs())

    {

    System.out.println("复制目录失败:创建目标目录失败!");

    return false;

    }

    }

    boolean flag = true;

    // 列出源文件夹下所有文件(包括子目录)的文件名

    File[] files = srcDir.listFiles();

    for (int i = 0; i < files.length; i++)

    {

    // 如果是一个单个文件,则进行复制

    if (files[i].isFile())

    {

    flag = CopyFileUtil.copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName());

    if (!flag)

    {

    break;

    }

    }

    // 如果是子目录,继续复制目录

    if (files[i].isDirectory())

    {

    flag = CopyFileUtil.copyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName());

    if (!flag)

    {

    break;

    }

    }

    }

    if (!flag)

    {

    System.out.println("复制目录" + srcDirName + "至" + destDirName + "失败!");

    return false;

    }

    System.out.println("复制目录" + srcDirName + "至" + destDirName + "成功!");

    return true;

    }

    /**

    * 复制单个文件, 如果目标文件存在,则不覆盖。

    *

    * @param srcFileName

    * 待复制的文件名

    * @param destFileName

    * 目标文件名

    * @return 如果复制成功,则返回true,否则返回false

    */

    public static boolean copyFile(String srcFileName, String destFileName)

    {

    return CopyFileUtil.copyFile(srcFileName, destFileName, false);

    }

    /**

    * 复制单个文件

    *

    * @param srcFileName

    * 待复制的文件名

    * @param destFileName

    * 目标文件名

    * @param overlay

    * 如果目标文件存在,是否覆盖

    * @return 如果复制成功,则返回true,否则返回false

    */

    public static boolean copyFile(String srcFileName, String destFileName, boolean overlay)

    {

    // 判断原文件是否存在

    File srcFile = new File(srcFileName);

    if (!srcFile.exists())

    {

    System.out.println("复制文件失败:原文件" + srcFileName + "不存在!");

    return false;

    }

    else if (!srcFile.isFile())

    {

    System.out.println("复制文件失败:" + srcFileName + "不是一个文件!");

    return false;

    }

    // 判断目标文件是否存在

    File destFile = new File(destFileName);

    if (destFile.exists())

    {

    // 如果目标文件存在,而且复制时允许覆盖。

    if (overlay)

    {

    // 删除已存在的目标文件,无论目标文件是目录还是单个文件

    System.out.println("目标文件已存在,准备删除它!");

    if (!DeleteFileUtil.delete(destFileName))

    {

    System.out.println("复制文件失败:删除目标文件" + destFileName + "失败!");

    return false;

    }

    }

    else

    {

    System.out.println("复制文件失败:目标文件" + destFileName + "已存在!");

    return false;

    }

    }

    else

    {

    if (!destFile.getParentFile().exists())

    {

    // 如果目标文件所在的目录不存在,则创建目录

    System.out.println("目标文件所在的目录不存在,准备创建它!");

    if (!destFile.getParentFile().mkdirs())

    {

    System.out.println("复制文件失败:创建目标文件所在的目录失败!");

    return false;

    }

    }

    }

    // 准备复制文件

    int byteread = 0;// 读取的位数

    InputStream in = null;

    OutputStream out = null;

    try

    {

    // 打开原文件

    in = new FileInputStream(srcFile);

    // 打开连接到目标文件的输出流

    out = new FileOutputStream(destFile);

    byte[] buffer = new byte[1024];

    // 一次读取1024个字节,当byteread为-1时表示文件已经读完

    while ((byteread = in.read(buffer)) != -1)

    {

    // 将读取的字节写入输出流

    out.write(buffer, 0, byteread);

    }

    System.out.println("复制单个文件" + srcFileName + "至" + destFileName + "成功!");

    return true;

    }

    catch (Exception e)

    {

    System.out.println("复制文件失败:" + e.getMessage());

    return false;

    }

    finally

    {

    // 关闭输入输出流,注意先关闭输出流,再关闭输入流

    if (out != null)

    {

    try

    {

    out.close();

    }

    catch (IOException e)

    {

    e.printStackTrace();

    }

    }

    if (in != null)

    {

    try

    {

    in.close();

    }

    catch (IOException e)

    {

    e.printStackTrace();

    }

    }

    }

    }

    public static void main(String[] args)

    {

    // 复制单个文件,如果目标存在,则覆盖

    String srcPath = "C:/temp/tempfile0.txt";

    String destPath = "C:/temp_bak/tempfile0_bak.txt";

    CopyFileUtil.copyFile(srcPath, destPath, true);

    // 如果目标存在,则不覆盖

    CopyFileUtil.copyFile(srcPath, destPath);

    System.out.println();

    // 复制文件夹,如果目标存在,则覆盖

    String srcDir = "C:/temp";

    String destDir = "D:/temp";

    CopyFileUtil.copyDirectory(srcDir, destDir, true);

    }

    }

    二、JAVA中怎么读取DAT文件中的内容

    DAT估计是个二进制 或者文本 跟普通读取文件是一样的

    读取上来 你再对文件格式进行拆分 首先你要了解 它的格式是什么  你可以用 

    NOTEPAD++或者 C32ASM打开 看看

    public class ReadFromFile {

        /**

         * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

         */

        public static void readFileByBytes(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;

            }

            try {

                System.out.println("以字节为单位读取文件内容,一次读多个字节:");

                // 一次读多个字节

                byte[] tempbytes = new byte[100];

                int byteread = 0;

                in = new FileInputStream(fileName);

                ReadFromFile.showAvailableBytes(in);

                // 读入多个字节到字节数组中,byteread为一次读入的字节数

                while ((byteread = in.read(tempbytes)) != -1) {

                    System.out.write(tempbytes, 0, byteread);

                }

            } catch (Exception e1) {

                e1.printStackTrace();

            } finally {

                if (in != null) {

                    try {

                        in.close();

                    } catch (IOException e1) {

                    }

                }

            }

        }

        /**

         * 以字符为单位读取文件,常用于读文本,数字等类型的文件

         */

        public static void readFileByChars(String fileName) {

            File file = new File(fileName);

            Reader reader = null;

            try {

                System.out.println("以字符为单位读取文件内容,一次读一个字节:");

                // 一次读一个字符

                reader = new InputStreamReader(new FileInputStream(file));

                int tempchar;

                while ((tempchar = reader.read()) != -1) {

                    // 对于windows下,rn这两个字符在一起时,表示一个换行。

                    // 但如果这两个字符分开显示时,会换两次行。

                    // 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。

                    if (((char) tempchar) != 'r') {

                        System.out.print((char) tempchar);

                    }

                }

                reader.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

            try {

                System.out.println("以字符为单位读取文件内容,一次读多个字节:");

                // 一次读多个字符

                char[] tempchars = new char[30];

                int charread = 0;

                reader = new InputStreamReader(new FileInputStream(fileName));

                // 读入多个字符到字符数组中,charread为一次读取字符数

                while ((charread = reader.read(tempchars)) != -1) {

                    // 同样屏蔽掉r不显示

                    if ((charread == tempchars.length)

                            && (tempchars[tempchars.length - 1] != 'r')) {

                        System.out.print(tempchars);

                    } else {

                        for (int i = 0; i < charread; i++) {

                            if (tempchars[i] == 'r') {

                                continue;

                            } else {

                                System.out.print(tempchars[i]);

                            }

                        }

                    }

                }

            } catch (Exception e1) {

                e1.printStackTrace();

            } finally {

                if (reader != null) {

                    try {

                        reader.close();

                    } catch (IOException e1) {

                    }

                }

            }

        }

        /**

         * 以行为单位读取文件,常用于读面向行的格式化文件

         */

        public static void readFileByLines(String fileName) {

            File file = new File(fileName);

            BufferedReader reader = null;

            try {

                System.out.println("以行为单位读取文件内容,一次读一整行:");

                reader = new BufferedReader(new FileReader(file));

                String tempString = null;

                int line = 1;

                // 一次读入一行,直到读入null为文件结束

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

                    // 显示行号

                    System.out.println("line " + line + ": " + tempString);

                    line++;

                }

                reader.close();

            } catch (IOException e) {

                e.printStackTrace();

            } finally {

                if (reader != null) {

                    try {

                        reader.close();

                    } catch (IOException e1) {

                    }

                }

            }

        }

        /**

         * 随机读取文件内容

         */

        public static void readFileByRandomAccess(String fileName) {

            RandomAccessFile randomFile = null;

            try {

                System.out.println("随机读取一段文件内容:");

                // 打开一个随机访问文件流,按只读方式

                randomFile = new RandomAccessFile(fileName, "r");

                // 文件长度,字节数

                long fileLength = randomFile.length();

                // 读文件的起始位置

                int beginIndex = (fileLength > 4) ? 4 : 0;

                // 将读文件的开始位置移到beginIndex位置。

                randomFile.seek(beginIndex);

                byte[] bytes = new byte[10];

                int byteread = 0;

                // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

                // 将一次读取的字节数赋给byteread

                while ((byteread = randomFile.read(bytes)) != -1) {

                    System.out.write(bytes, 0, byteread);

                }

            } catch (IOException e) {

                e.printStackTrace();

            } finally {

                if (randomFile != null) {

                    try {

                        randomFile.close();

                    } catch (IOException e1) {

                    }

                }

            }

        }

        /**

         * 显示输入流中还剩的字节数

         */

        private static void showAvailableBytes(InputStream in) {

            try {

                System.out.println("当前字节输入流中的字节数为:" + in.available());

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        public static void main(String[] args) {

            String fileName = "C:/temp/newTemp.txt";

            ReadFromFile.readFileByBytes(fileName);

            ReadFromFile.readFileByChars(fileName);

            ReadFromFile.readFileByLines(fileName);

            ReadFromFile.readFileByRandomAccess(fileName);

        }

    }

    三、C# 连续读取数据库中的文件流

    C#读写txt文件的两种方法:

    1.添加命名空间

    System.IO;

    System.Text;

    2.文件的读取

    (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。

    byte[] byData = new byte[100];

    char[] charData = new char[1000];

    public void Read()

    {

    try

    {

    FileStream file = new FileStream("E:\\test.txt", FileMode.Open);

    file.Seek(0, SeekOrigin.Begin);

    file.Read(byData, 0, 100); //byData传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.

    Decoder d = Encoding.Default.GetDecoder();

    d.GetChars(byData, 0, byData.Length, charData, 0);

    Console.WriteLine(charData);

    file.Close();

    }

    catch (IOException e)

    {

    Console.WriteLine(e.ToString());

    }

    }

    (2).使用StreamReader读取文件,然后一行一行的输出。

    public void Read(string path)

    {

    StreamReader sr = new StreamReader(path,Encoding.Default);

    String line;

    while ((line = sr.ReadLine()) != null)

    {

    Console.WriteLine(line.ToString());

    }

    }

    3.文件的写入

    (1).使用FileStream类创建文件,然后将数据写入到文件里。

    public void Write()

    {

    FileStream fs = new FileStream("E:\\ak.txt", FileMode.Create);

    //获得字节数组

    byte[] data = System.Text.Encoding.Default.GetBytes("Hello World!");

    //开始写入

    fs.Write(data, 0, data.Length);

    //清空缓冲区、关闭流

    fs.Flush();

    fs.Close();

    }

    (2).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。

    public void Write(string path)

    {

    FileStream fs = new FileStream(path, FileMode.Create);

    StreamWriter sw = new StreamWriter(fs);

    //开始写入

    sw.Write("Hello World!!!!");

    //清空缓冲区

    sw.Flush();

    //关闭流

    sw.Close();

    fs.Close();

    }

    四、用C++读取一个文件,文件前4byte是int型然后是char型等等,如何分类读取?

    正常的编译器int都是四个字节,就直接读入一个int变量就可以了,然后再读入一个char变量。

    就直接赋值给变量,编译去会自己安排字节数的。

    如果按字节读入的话,还要重新排序,高高低低,这个有点麻烦。

    以上就是关于chars怎么读相关问题的回答。希望能帮到你,如有更多相关问题,您也可以联系我们的客服进行咨询,客服也会为您讲解更多精彩的知识和内容。


    推荐阅读:

    ChatGPT受益分支!AI大模型引科技巨头争相入局,这些上市公司有相关业务

    最纯ChatGPT概念股(cnt概念股)

    mchat安卓怎么用不了(mchat怎么下载不了)

    迷你世界图标变化(迷你世界图标变化怎么弄)

    营业厅吸引人气活动