使用 DataOutputStream 确定写入文件的字节数的Java程序
Java OutputStream 类, Java.io.OutputStream ,是Java IO API中所有输出流的基类。输出流接受输出字节并将它们发送到某个接收器。需要定义 OutputStream 子类的应用程序必须始终提供至少一种写入一个字节输出的方法。
- 为了获得写入的字节数,我们首先创建一个对象 FileOutputStream通过传递在下面的代码中显示为 FileOS 的文件路径。
- 然后我们通过传递 FileOS 即 FileOutputStream 的对象来创建DataOutputStream的对象,它在下面的代码中显示为 DataOS。
- 现在,我们将使用 DataOutputStream 的writeBytes()方法将文本注入到该文本文件中。为了获得写入的字节大小,我们在对象 DataOS 上使用 DataOutputStream 类的 size 方法。
方案一:
Java
// Java program to determine number of bytes
// written to DataOutputStream
import java.io.*;
public class NumberOfBytesInOutputStream {
public static void main(String[] args)
{
try {
// creates object of FileOutputStream by passing
// file Bytes.txt
FileOutputStream FileOS
= new FileOutputStream("C:/Bytes.txt");
// creates object of DataOutputStream by
// passing object of FileOutputStream i.e.
// FileOS
DataOutputStream DataOS = new DataOutputStream(FileOS);
// writes the string passed to object of
// DataOutputStream
DataOS.writeBytes(
"GeeksforGeeks is the best place to learn Coding online.");
// Stores the number of bytes to total_bytes
// variable using size() method of
// DataOutputStream class
int total_bytes = DataOS.size();
// Showing the number of bytes as output in
// console
DataOS.close();
System.out.println("Total " + total_bytes
+ " bytes were written to stream.");
}
catch (Exception e)
{
System.out.println("Exception: " + e.toString());
}
}
}
Java
// Java program to determine number of bytes
// written to DataOutputStream
import java.io.*;
public class NumberOfBytesInOutputStream2 {
public static void main(String[] args)
{
try {
// creates object of FileOutputStream by passing
// file Bytes.txt
FileOutputStream FileOS = new FileOutputStream("C:/NumberOfBytes.txt");
// creates object of DataOutputStream by passing
// object of FileOutputStream i.e. FileOS
DataOutputStream DataOS = new DataOutputStream(FileOS);
// create string S with the desired text
String S = "GeeksforGeeks article is best for getting cs concepts.";
// writes the string passed to object of
// DataOutputStream
DataOS.writeBytes(S);
// Stores the number of bytes to total_bytes
// variable using size() method of
// DataOutputStream class
int total_bytes = DataOS.size();
// Showing the number of bytes as output in
// console
System.out.println("Total " + total_bytes
+ " bytes were written to stream.");
DataOS.close();
}
catch (Exception e)
{
System.out.println("Exception: " + e.toString());
}
}
}
输出:
方案二:
Java
// Java program to determine number of bytes
// written to DataOutputStream
import java.io.*;
public class NumberOfBytesInOutputStream2 {
public static void main(String[] args)
{
try {
// creates object of FileOutputStream by passing
// file Bytes.txt
FileOutputStream FileOS = new FileOutputStream("C:/NumberOfBytes.txt");
// creates object of DataOutputStream by passing
// object of FileOutputStream i.e. FileOS
DataOutputStream DataOS = new DataOutputStream(FileOS);
// create string S with the desired text
String S = "GeeksforGeeks article is best for getting cs concepts.";
// writes the string passed to object of
// DataOutputStream
DataOS.writeBytes(S);
// Stores the number of bytes to total_bytes
// variable using size() method of
// DataOutputStream class
int total_bytes = DataOS.size();
// Showing the number of bytes as output in
// console
System.out.println("Total " + total_bytes
+ " bytes were written to stream.");
DataOS.close();
}
catch (Exception e)
{
System.out.println("Exception: " + e.toString());
}
}
}
输出: