精华内容
下载资源
问答
  • java二进制流读取文件
    2021-02-28 12:22:35

    代码如下:

    public static byte bSource;

    public static byte bTarget;

    public static byte colon = ':';

    /**

    * 从二进制文件读取字节数组

    *

    * @param sourceFile

    * @return

    * @throws IOException

    */

    public static byte[] readFile(File sourceFile) {

    if (sourceFile.isFile() && sourceFile.exists()) {

    long fileLength = sourceFile.length();

    if (fileLength > 0L) {

    try {

    BufferedInputStream fis = new BufferedInputStream(

    new FileInputStream(sourceFile));

    byte[] b = new byte[(int) fileLength];

    while (fis.read(b) != -1) {

    }

    fis.close();

    fis = null;

    return b;

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    return null;

    }

    /**

    * 将字节数组读入二进制文件

    *

    * @param targetFile

    * @param content

    * @return

    */

    public static boolean writeBytes(File targetFile, byte[] content) {

    try {

    BufferedOutputStream fos = new BufferedOutputStream(

    new FileOutputStream(targetFile));

    for (int i = 0; i < content.length - 1; i++) {

    // 替换掉盘符路径(E:) 以冒号作为判断改字节是否代表一个盘符

    if (content[i] == bSource && content[i + 1] == colon) {

    content[i] = bTarget;

    }

    fos.write(content[i]);

    }

    fos.write(content[content.length - 1]); // 写入最后一个字节

    fos.flush();

    fos.close();

    return true;   } catch (IOException e) {    e.printStackTrace();   }   return false;  }

    更多相关内容
  • JAVA二进制读写库(读取)
  • 本篇文章主要介绍了Java实现较大二进制文件的读、写方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileOperation { public static void...
  • Java二进制流

    2021-03-22 13:26:40
    1-二进制流的概述二进制流有两个顶级类:InputStream和OutputStream,下面的两个类是各种附属类。作为对比,二进制流的关系比字符的更加多样化和复杂。关于二进制流,LineNumberInputStream和...

    1-二进制流的概述

    二进制流有两个顶级类:InputStream和OutputStream, 下面的两个类是各种附属类。作为对比,二进制流的关系比字符流的更加多样化和复杂。关于二进制流,LineNumberInputStream和StringBufferInputStream两个类在JDK1.5中,尽量不要使用,因为它们已被弃用。

    2- InputStream & OutputStream类

    InputStream类是一个抽象类,所以您不能在 InputStream 类本身创建输出流对象。然而,这个类分为由关联类继承许多分支。在特定情况下,您可以从构造函数创建InputStream派生类的对象。

    // Java.io.InputStream is an abstract class

    // Could not initialize InputStream object directly, through class InputStream.

    // Initialize InputStream object should be through its subclasses ..

    InputStream fileStream =new FileInputStream("C:/test.txt");

    // Input stream from the keyboard ..

    InputStream is = System.in;

    OutputStream类是一个抽象类,所以你无法通过的OutputStream类来创建输出流对象。然而,这个类分割成子分支发挥了重要作用。在某些情况下,您可以从子类的构造函数创建InputStream对象。

    // Java.io.OutputStream is an abstract class

    // Could not initialize OutputStream object directly, through class OutputStream.

    // Initialize OutputStream object should be through its subclasses ..

    // Stream write to file.

    OutputStream os=new FileOutputStream("D:/outData.txt");

    // Stream write to console.

    OutputStream w=System.out;

    0f5d317cbcacd7537fd3a7c3b7371380.png

    HelloInputStream.java

    package com.yiibai.tutorial.javaio.stream;

    import java.io.FileInputStream;

    import java.io.InputStream;

    public class HelloInputStream {

    public static void main(String[] args) {

    try {

    // Create InputStream object from subclass.

    // This is Stream read file.

    InputStream is = new FileInputStream("data.txt");

    int i = -1;

    // Read the turn of bytes in the stream.

    // Each time the 8-bit read, convert it to int.

    // Read the value of -1 means the end of the stream.

    while ((i = is.read()) != -1) {

    System.out.println(i + " " + (char) i);

    }

    is.close();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    输出结果如下:

    12faaa46055667e0e492b6debc694189.png

    HelloOutputStream.java

    package com.yiibai.tutorial.javaio.stream;

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.OutputStream;

    public class HelloOutputStream {

    public static void main(String[] args) {

    try {

    File dir = new File("C:/Test");

    // Create directories if not exists

    dir.mkdirs();

    // Create output Stream write data to file.

    OutputStream w = new FileOutputStream(

    "C:/Test/test_outputStream.txt");

    // Create array of bytes, write it to stream.

    byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o' };

    // write turn the bytes into the stream

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

    byte b = by[i];

    // Write 1 byte.

    w.write(b);

    }

    // Close the output stream, finish write file.

    w.close();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    输出结果如下:

    d029196e0e7b710b16afb570e116c05e.png

    以上两个例子是简单的。以上两个例子是简单的。它们读或写每个字节。在下面的例子中,它们读或写同步不同的字节。这有助于提高处理速度。

    InputStreamExample2.java

    package com.yiibai.tutorial.javaio.stream;

    import java.io.FileInputStream;

    import java.io.InputStream;

    public class InputStreamExample2 {

    public static void main(String[] args) {

    try {

    // Create input stream, read a file.

    InputStream in = new FileInputStream("data.txt");

    // A temporary array to store data each reading

    byte[] temp = new byte[10];

    int i = -1;

    // Reads some number of bytes from the input stream

    // and stores them into the buffer array 'temp'.

    // Return the number of bytes actually read.

    // return -1 if end of stream.

    while ((i = in.read(temp)) != -1) {

    // Create String from bytes

    String s = new String(temp, 0, i);

    System.out.println(s);

    }

    in.close();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    在下面图片的例子中说明了同时进行读取:

    987bcfe36bbebde84ceb2231a2e7715e.png

    OutputStreamExample2.java

    package com.yiibai.tutorial.javaio.stream;

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.OutputStream;

    public class OutputStreamExample2 {

    public static void main(String[] args) {

    try {

    File dir = new File("C:/Test");

    // Create directories if not exists.

    dir.mkdirs();

    // Create output Stream to write file.

    OutputStream os = new FileOutputStream("C:/Test/test_writerOutputStream.txt");

    // Create array of bytes, write bytes into the file above.

    byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 31, 34, 92 };

    byte[] by2 = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'b', 'o',

    'y' };

    // Write all of bytes in array into Stream.

    os.write(by);

    // Flush data in memory to file.

    os.flush();

    // Continue write the 2nd byte array to the stream

    os.write(by2);

    // Close the output stream, finish write file.

    os.close();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    3- ByteArrayInputStream & ByteArrayOutputStream类

    ByteArrayInputStream包装字节数组(byte[] buf),并通过ByteArrayInputStream进行访问数组的元素。

    ByteArrayOutputStream是一个字节流,其中包含的一个的数组字节流(byte[]buf) 能够通过自身数量的增加,以增加字节的大小。 每个字节每次被写入流时,这意味着分配这些字节到的数组。

    当数组是充分分配元素,程序创建旧数组在一个新较长数组并拷贝元素为...(这是它自己的增加的字节数组的大小 - 如上所述)

    ByteArrayOutputStream的一些方法:

    // return the current contents of this output stream, as a byte array.

    - byte[] toByteArray();

    // String decoded from the buffer's contents.

    - String toString() ;

    // return the number of valid bytes in this output stream.

    - int size();

    ByteArrayInputStreamExample.java

    package com.yiibai.tutorial.javaio.bytestream;

    import java.io.ByteArrayInputStream;

    import java.io.IOException;

    public class ByteArrayInputStreamExample {

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

    // Byte Array.

    byte[] bytes = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'I', 'O' };

    // Using ByteArrayInputStream to read bytes array.

    ByteArrayInputStream bInput = new ByteArrayInputStream(bytes);

    System.out.println("Converting characters to Upper case ");

    int c = 0;

    // Read the turn of bytes in the stream.

    // Cursor will move from the beginning to the end of the array array.

    // Every time you read a byte pointer will move one step to the end.

    while ((c = bInput.read()) != -1) {

    char ch = (char) c;

    ch = Character.toUpperCase(ch);

    System.out.println(ch);

    }

    // Check whether this stream supports mark or not

    boolean markSupport = bInput.markSupported();

    System.out.println("Mark Support? " + markSupport);

    // Move the cursor to the default location

    // In this example, it will move to position 0 ..

    bInput.reset();

    char ch = (char) bInput.read();

    System.out.println(ch);

    // Read next byte

    ch = (char) bInput.read();

    System.out.println(ch);

    System.out.println("Skip 4");

    // Skip 4 bytes

    bInput.skip(4);

    ch = (char) bInput.read();

    System.out.println(ch);

    }

    }

    输出结果:

    cdda14ad9968a06f21d9832c0d4c1046.png

    ByteArrayOutputStreamExample.java

    package com.yiibai.tutorial.javaio.bytestream;

    import java.io.ByteArrayOutputStream;

    import java.io.IOException;

    public class ByteArrayOutputStreamExample {

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

    // Create ByteArrayOutputStream object.

    // Object contains within it an array of bytes.

    // Array with size 12 elements.

    // If the number of elements to write to stream more than 12, the array will be replaced by

    // new array has more elements, and copy the elements of old array into.

    ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);

    String s = "Hello ByteArrayOutputStream";

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

    char ch = s.charAt(i);

    if (ch != 'a' && ch != 'e') {

    bOutput.write(ch);

    }

    }

    // Returns the current size of the buffer.

    int size = bOutput.size();

    System.out.println("Size = " + size);

    byte[] bytes = bOutput.toByteArray();

    String ss = new String(bytes);

    System.out.println("New String = " + ss);

    }

    }

    输出结果:

    2067196206c8519be1b68379305a2f70.png

    4- ObjectInputStream和ObjectOutputStream类

    ObjectInputStream和ObjectOutputStream,并允许您对读取或写入流中。这些对象必须是可序列化的类型(这意味着它们可布置成队列)。

    这里有些例子:

    Student.java

    package com.yiibai.tutorial.javaio.objstream;

    import java.io.Serializable;

    public class Student implements Serializable {

    private static final long serialVersionUID = -5074534753977873204L;

    private String firstName;

    private String lastName;

    public Student(String firstName, String lastName) {

    this.firstName = firstName;

    this.lastName = lastName;

    }

    public String getFirstName() {

    return firstName;

    }

    public void setFirstName(String firstName) {

    this.firstName = firstName;

    }

    public String getLastName() {

    return lastName;

    }

    public void setLastName(String lastName) {

    this.lastName = lastName;

    }

    }

    Pupil.java

    package com.yiibai.tutorial.javaio.objstream;

    import java.io.Serializable;

    public class Pupil implements Serializable {

    private static final long serialVersionUID = -8501383434011302991L;

    private String fullName;

    public Pupil(String fullName) {

    this.fullName= fullName;

    }

    public String getFullName() {

    return fullName;

    }

    public void setFullName(String fullName) {

    this.fullName = fullName;

    }

    }

    ObjectOutputStreamExample.java

    package com.yiibai.tutorial.javaio.objstream;

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectOutputStream;

    import java.util.Date;

    public class ObjectOutputStreamExample {

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

    File dir = new File("C:/Test");

    // Create directories if not exists.

    dir.mkdirs();

    // Create stream write to file.

    FileOutputStream fos = new FileOutputStream(

    "C:/Test/testObjectStream.txt");

    // Create ObjectOutputStream object wrap 'fos'.

    // Data written to this stream will be pushed to 'fos'.

    ObjectOutputStream oos = new ObjectOutputStream(fos);

    // Write String to Stream.

    oos.writeUTF("This is student, pupil profiles");

    // Note: Write Serializable object only.

    // Write an Object to stream.

    oos.writeObject(new Date());

    Student student1 = new Student("Thanh", "Phan");

    Student student2 = new Student("Ngan", "Tran");

    Pupil pupil1 = new Pupil("Nguyen Van Ba");

    oos.writeObject(student1);

    oos.writeObject(pupil1);

    oos.writeObject(student2);

    oos.close();

    System.out.println("Write successful");

    }

    }

    运行结果:

    b0147a12bae81c6279e4d88d32dd876f.png

    这是一个关于文件写入对象的图示。它被依次处理。在未来阅读时,你要记住写这样可以读取它正确的顺序。

    d7665b44552d6f25868dedfeedb307ae.png

    这是上述实例中提到的 ObjectInputStream EOF 读取文件的一个例子:

    ObjectInputStreamExample.java

    package com.yiibai.tutorial.javaio.objstream;

    import java.io.FileInputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.util.Date;

    public class ObjectInputStreamExample {

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

    ClassNotFoundException {

    // Create stream to read file.

    FileInputStream fis = new FileInputStream(

    "C:/Test/testObjectStream.txt");

    // Create ObjectInputStream object wrap 'fis'.

    ObjectInputStream ois = new ObjectInputStream(fis);

    // Read String.

    String s = ois.readUTF();

    System.out.println(s);

    // Read Object.

    Date date = (Date) ois.readObject();

    System.out.println("Date = " + date);

    Student student1 = (Student) ois.readObject();

    System.out.println("Student " + student1.getFirstName());

    Pupil pupil = (Pupil) ois.readObject();

    System.out.println("Pupil " + pupil.getFullName());

    Student student2 = (Student) ois.readObject();

    System.out.println("Student " + student2.getFirstName());

    ois.close();

    }

    }

    运行结果:

    f05325fbe7ad24d9110b303c2174e426.png

    5- Class DataInputStream 和 DataOutputStream

    DataInputStream

    // Constructor

    public DataOutputStream(OutputStream out)

    // Write a character 16 bit (2-byte)

    public void writeChar(int val)

    // Write double 64 bit (8-byte)

    public void writeDouble(double val)

    // Write float 32 bit (4-byte)

    public void writeFloat(float val)

    // Write integer 32 bit (4-byte)

    public void writeInt(int val)

    // Write String UTF-8.

    public void writeUTF(String obj)

    ....

    DataInputStream

    // Constructor

    public DataInputStream(InputStream in)

    // Read a character 16 bit (2 byte)

    public char readChar()

    // Read double 64 bit (8 byte)

    public double readDouble()

    // Read float 32 bit (4 byte)

    public float readFloat()

    // Read int 16 bit (4 byte)

    public int readInt()

    // Read UTF-8 String.

    public String readUTF()

    ....

    DataOutputStreamExample.java

    package com.yiibai.tutorial.javaio.datastream;

    import java.io.DataOutputStream;

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.IOException;

    public class DataOutputStreamExample {

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

    int cityIdA = 1;

    String cityNameA = "Green Lake City";

    int cityPopulationA = 500000;

    float cityTempA = 15.50f;

    int cityIdB = 2;

    String cityNameB = "Salt Lake City";

    int cityPopulationB = 250000;

    float cityTempB = 10.45f;

    File dir = new File("C:/Test");

    dir.mkdirs();

    //

    // Create FileOutputStream write to file.

    //

    FileOutputStream fos = new FileOutputStream("C:/Test/cities.txt");

    // Create DataOutputStream object wrap 'fos'.

    // The data write to 'dos' will be pushed to 'fos'.

    DataOutputStream dos = new DataOutputStream(fos);

    //

    // Write data.

    //

    dos.writeInt(cityIdA);

    dos.writeUTF(cityNameA);

    dos.writeInt(cityPopulationA);

    dos.writeFloat(cityTempA);

    dos.writeInt(cityIdB);

    dos.writeUTF(cityNameB);

    dos.writeInt(cityPopulationB);

    dos.writeFloat(cityTempB);

    dos.flush();

    dos.close();

    }

    }

    运行 DataOutputStream 类实例类,并收到一个写出来数据文件。

    DataInputStreamExample.java

    package com.yiibai.tutorial.javaio.datastream;

    import java.io.DataInputStream;

    import java.io.FileInputStream;

    import java.io.IOException;

    public class DataInputStreamExample {

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

    // Stream to read file.

    FileInputStream fis = new FileInputStream("C:/Test/cities.txt");

    // Create DataInputStream object wrap 'fis'.

    DataInputStream dis = new DataInputStream(fis);

    //

    // Read data.

    //

    int cityId1 = dis.readInt();

    System.out.println("Id: " + cityId1);

    String cityName1 = dis.readUTF();

    System.out.println("Name: " + cityName1);

    int cityPopulation1 = dis.readInt();

    System.out.println("Population: " + cityPopulation1);

    float cityTemperature1 = dis.readFloat();

    System.out.println("Temperature: " + cityTemperature1);

    //

    // Read data.

    //

    int cityId2 = dis.readInt();

    System.out.println("Id: " + cityId2);

    String cityName2 = dis.readUTF();

    System.out.println("Name: " + cityName2);

    int cityPopulation2 = dis.readInt();

    System.out.println("Population: " + cityPopulation2);

    float cityTemperature2 = dis.readFloat();

    System.out.println("Temperature: " + cityTemperature2);

    dis.close();

    }

    }

    运行结果:

    182eecc838ac288c7ad30e671be9e6c3.png

    6- SequenceInputStream

    现在你已经熟悉了读取一个文件,并知道如何得到一个输入流。然而,你有时需要读取很多文件并加入数据写入另一个文件中,例如。这意味着你以串联小数据流方式,加入许多输入流在一起用来创建一个更大的流。我们现在讨论 java.io.SequenceInputStream 中的类。没有对应的定义输出流...

    526dadcc50350eb9b31b108a5d85f681.png

    // Constructor

    // Create new Stream from Pairing two streams together

    // which will be read in order

    public SequenceInputStream(InputStream s1,InputStream s2)

    // Create new Stream from Multi input stream.

    // which will be read in order

    public SequenceInputStream(Enumeration extends InputStream> e)

    示例:

    // Input stream read a file - File1.txt .

    InputStream is1=new FileInputStream("File1.txt");

    // Input stram read a file - File2.txt

    InputStream is2=new FileInputStream("File2.txt");

    // Create new Stream from two stream

    SequenceInputStream sis=new SequenceInputStream(is1,is2);

    7- PipedInputStream,PipedOutputStream

    1109efeae37918980187e6b1c2f9c44c.png

    1ff297e1ca94569811189f2e9d0bf651.png

    8- Class PrintStream

    6fa04aeebdb4d8cfe5c12e527faff533.png

    // PrintStream is the subclass of FilterOutputStream.

    // It can wrap a binary output stream (OutputStream) , ..

    // Constructor :

    // Wrap a OutputStream

    public PrintStream(OutputStream out)

    public PrintStream(OutputStream out,boolean autoFlush)

    // Write to file ..

    public PrintStream(String fileName)

    // Some methods

    public void println(String s)

    public void print(char ch)

    // Write an Object

    public void print(Object obj)

    // Write long value (64bit)

    public void print(long n)

    public PrintStream append(java.lang.CharSequence csq) .

    // ... (more see javadoc)

    现在我们已经知道如何通过 try-catch 捕捉异常。

    try {

    // Do something here

    // Error divided by 0

    int i=10/0;

    }

    // The catch block is executed

    catch(Exception e) {

    // Print out message

    System.out.println("Error on try..."+e.getMessage());

    // Print 'stack trace' to Console.

    // How to get the text "stack trace"?

    e.printStackTrace();

    }

    这是你经常看到的“堆栈跟踪”,当有东西出错时。

    下面的示例是用来检索字符串“堆栈跟踪”

    GetStackTraceString.java

    package com.yiibai.tutorial.javaio.printstream;

    import java.io.ByteArrayOutputStream;

    import java.io.PrintStream;

    public class GetStackTraceString {

    private static String getStackTraceString(Exception e) {

    // Create ByteArrayOutputStream

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Data written to this stream will be pushed to 'baos'.

    PrintStream printStream = new PrintStream(baos);

    // Prints this throwable and its backtrace to 'printStream'

    e.printStackTrace(printStream);

    printStream.close();

    byte[] bytes = baos.toByteArray();

    String s = new String(bytes);

    return s;

    }

    public static void main(String[] args) {

    try {

    // Do something here

    // Error divided by 0

    int i = 10 / 0;

    }

    // The catch block is executed

    catch (Exception e) {

    // Print out message

    System.out.println("Error on try..." + e.getMessage());

    // Get the text "stack trace"

    String s = getStackTraceString(e);

    System.out.println("Stack Trace String " + s);

    }

    }

    }

    下面是打印“堆栈跟踪”到控制台的代码,但使用printStackTrace(PrintStream)方法,而不是使用printStackTrace()方法,默认为打印“堆栈跟踪”到控制台。

    // PrintStream to write to the Console

    PrintStream os = System.out;

    // Exception e ..

    // Write 'stack trace' to 'os'. This means print to the Console.

    e.printStackTrace(os);

    // In essence it is equivalent to calling:

    e.printStackTrace();

    ¥ 我要打赏

    纠错/补充

    收藏

    加QQ群啦,易百教程官方技术学习群

    注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

    展开全文
  • 主要给大家介绍了利用Java读取二进制文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用java具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
  • 本篇文章主要介绍了JAVA读取文件二进制,字符)内容的方法总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • java读写二进制流

    2021-02-12 12:55:50
    写入public static void main(String[] args) throws FileNotFoundException, IOException {ObjectOutputStream...try{//创建ObjectOutputStream输出oos=new ObjectOutputStream(new FileOutputStream("d:\\test\\...

    写入

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

    ObjectOutputStream oos=null;

    try{

    //创建ObjectOutputStream输出流

    oos=new ObjectOutputStream(new FileOutputStream("d:\\test\\student.txt"));

    Student stu=new Student("小明",22,"男","123456");

    //对象序列化,写入输出流

    oos.writeObject(stu);

    }catch(IOException ex){

    ex.printStackTrace();

    }finally{

    if(oos!=null){

    oos.close();

    }

    }

    }

    读出

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

    ObjectInputStream ois=null;

    try{

    //创建ObjectOutputStream输出流

    ois=new ObjectInputStream(new FileInputStream("d:\\test\\student.txt"));

    //反序列化,强转类型

    Student stu=(Student)ois.readObject();

    //输出生成后对象信息

    System.out.println("姓名为:"+stu.getName());

    System.out.println("年龄为:"+stu.getAge());

    System.out.println("性别为:"+stu.getGender());

    System.out.println("密码为:"+stu.getpassword());

    }catch(IOException ex){

    ex.printStackTrace();

    }finally{

    if(ois!=null){

    ois.close();

    }

    }

    }

    实体类

    public class Student implements java.io.Serializable  {

    private String name;

    private int age;

    private String gender;

    //transient使password不能序列化,所以不能从输入流中读出,该属性在控制台输出是null

    private transient String password;

    public Student(String name, int age,String gender,String password){

    System.out.println("带参数的构造方法");

    this.name=name;

    this.age=age;

    this.gender=gender;

    this.password=password;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

    public String getGender() {

    return gender;

    }

    public void setGender(String gender) {

    this.gender = gender;

    }

    public String getpassword() {

    return password;

    }

    public void setpassword(String password) {

    this.password = password;

    }

    }

    展开全文
  • File I/O中常见的文件读写:1.字节读写文本文件FileInputStream;FileOutputStream;...3.二进制读写文件DataInputStream;DataOutputStream;这里重点介绍二进制文件的读写:一、使用字节类DataInputSt...

    File I/O中常见的文件读写:

    1.字节流读写文本文件

    FileInputStream;

    FileOutputStream;

    2.字符流读写文本文件

    FileReader;

    FileWriter;

    BufferedReader;

    BufferedWriter;

    3.二进制读写文件

    DataInputStream;

    DataOutputStream;

    这里重点介绍二进制文件的读写:

    一、使用字节流类DataInputStream读写二进制文件

    DataInputStream类是FileInputstream的子类,它是FileInputStream类的扩展。

    利用DataInputStream类读取二进制文件要使用到FileInputStream类。

    具体操作步骤:

    1)引入相关的类

    2)构造一个数据流对象

    3)利用数据输入流类的方法读取二进制文件的数据

    dis.read();//读取字节数据

    4)关闭数据输入流

    dis.close();//关闭数据输入流

    二、使用字节流类DataOutputStream写二进制文件

    DataOutputStream类是FileOutputStream的子类,需要用到FileOutputStream类。

    具体操作步骤:

    1)引入相关的类

    2)构造一个数据输出流对象

    3)利用数据输出流类的方法写二进制文件的数据

    out.write(1);//将指定字节数据写入二进制文件

    4)关闭数据输出流

    out.close();

    三、编写一个Java程序读取windows目录下的win.ini文件,并输出其内容

    1 importjava.io.DataInputStream;2 importjava.io.FileInputStream;3 importjava.io.FileNotFoundException;4 importjava.io.IOException;5

    6 /**

    7 * 编写一个Java程序读取windows目录下的win.ini文件,并输出其内容8 *@authorAdministrator9 *10 */

    11 public classTest041 {12

    13 public static voidmain(String[] args) {14 DataInputStream dis = null;15 try{16 /*创建二进制输入流*/

    17 dis = new DataInputStream(new FileInputStream("C:\\windows\\win.ini"));18

    19 /*循环读取并输出信息*/

    20 inttemp;21 while((temp=dis.read())!=-1){22 System.out.print((char)temp);23 }24 } catch(FileNotFoundException e) {25 e.printStackTrace();26 } catch(IOException e) {27 e.printStackTrace();28 }29 finally{30 if (dis!=null) {31 try{32 dis.close();33 } catch(IOException e) {34 e.printStackTrace();35 }36 }37 }38

    39 }40

    41 }

    四、DataInputStream类与DataOutputStream类搭配使用,可以按照与平台无关的方式从流中读取基本数据类型的数据

    1.DataInputStream的readUTF()方法能读取采用utf-8字符编码的字符串;

    2.DataOutputStream的writeUTF()方法能写入采用utf-8字符编码的字符串;

    test:复制图片

    1 importjava.io.DataInputStream;2 importjava.io.DataOutputStream;3 importjava.io.FileInputStream;4 importjava.io.FileOutputStream;5 importjava.io.IOException;6

    7 /**

    8 * 复制图片(二进制字节流)9 *@authorAdministrator10 *11 */

    12 public classTest004 {13

    14 public static voidmain(String[] args) {15 FileInputStream fis = null;16 FileOutputStream fos = null;17 DataInputStream dis = null;18 DataOutputStream dos = null;19 try{20 //创建输入流

    21 fis = new FileInputStream("D:\\tengyicheng\\timg.jpg");22 dis = newDataInputStream(fis);23 //创建输出流

    24 fos = new FileOutputStream("D:\\tengyicheng\\myFile\\timg.jpg");25 dos = newDataOutputStream(fos);26 //循环读取录入

    27 inttemp;28 while((temp = dis.read())!=-1){29 dos.write(temp);30 }31 } catch(IOException e) {32 e.printStackTrace();33 }34 finally{35 try{36 if (fis!=null) {37 fis.close();38 }39 if (fos!=null) {40 fos.close();41 }42 if (dis!=null) {43 dis.close();44 }45 if (dos!=null) {46 dos.close();47 }48 } catch(IOException e) {49 e.printStackTrace();50 }51 }52

    53 }54

    55 }

    展开全文
  • } } I know my code is not very good, I'm relatively new to Java so I don't know many others ways to accomplish this. 解决方案 Use Output stream instead of Writer as writer is not supposed to be used ...
  • Java读取文件二进制及文本文件

    千次阅读 2021-02-12 10:45:48
    读取二进制文件 读取二进制文件,并存入byte数组,如算法训练模型的二进制模型。 public byte[] readFromByteFile(String pathname) throws IOException{ File filename = new File(pathname); ...
  • 将做工程过程中比较好的一些内容段做个备份,下面的资料是关于 java分别通过字节、字符二进制读取文件的内容,应该是对小伙伴们有些用途。public class Start{public static void main(String args[]) throws ...
  • 本篇文章主要介绍了java实现解析二进制文件的方法(字符串、图片),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • /*** 二进制读写文件*/import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io...
  • 展开全部JAVA读取文件内容的方32313133353236313431303231363533e58685e5aeb931333365663531法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的...
  • 控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字。import java.nio.file.*;import java.nio.*;import java.io.*;public class StreamInputFromFile {public static void main...
  • Java 二进制数据转成文件

    千次阅读 2021-02-12 15:46:50
    importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importorg.slf4j.Logg...
  • Java二进制文件读写

    2020-08-31 20:33:11
    -二进制读写 1、输出数据到文件中 2、从文件读取数据 FileOutputStream:节点类,负责写字节 BufferedOutputStream:装饰类,负责写字节数据到缓冲区 DataOutputStream:转化类,负责数据类型到字节转化 import ...
  • Java读取二进制文件读取文件的前64和文件末尾64个字节。
  • Java读取和写入二进制文件的方法,应该说是一个基于二进制的大文件块,演示了如何自定义缓存区,并使用不具有缓冲功能的DataInputStream 和DataOutputStream 类来读写二进制的大文件块数据,运行效果请参见如下...
  • 用于读取二进制文件,可进行二进制数据检索等。
  • C#上传文件二进制流的形式上传到服务器,并从服务器下载二进制流文件到本地
  • Java二进制方式读取文件
  • java 二进制文件读写操作使用FileInputStream FileOutputStream
  • Java如何实现读取二进制文件

    千次阅读 2021-03-11 17:01:32
    Java如何实现读取二进制文件发布时间:2020-11-10 16:19:46来源:亿速云阅读:83作者:Leah这篇文章运用简单易懂的例子给大家介绍Java如何实现读取二进制文件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对...
  • Java读写文件,只能以(数据)的形式进行读写 java.io中包括字节、字符、其他(System)、文件处理 java.io包中 节点类:直接对文件进行读写 包装类 转化类:字节/字符/数据类型的转化类 装饰类:装饰节点类...
  • 主要介绍了java判断一个文件是否为二进制文件的方法,涉及java针对文件读取及编码判断技巧,具有一定参考借鉴价值,需要的朋友可以参考下

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 198,786
精华内容 79,514
关键字:

java二进制流读取文件