package com.hotfey.demo; import static com.hotfey.util.IOUtil.closeIO; import java.io.*; import java.util.logging.Logger; public class InputDemo { private static Logger logger = Logger.getLogger("JavaIODemo"); private static Reader reader; private static InputStream inputStream; private static BufferedReader bufferedReader; public static void main(String[] args) { String pathname = "D:/test.txt"; File file = new File(pathname); inputStream(file); inputStream2Reader(file); inputStream2BufferedReader(file); } private static void inputStream(File file) { try { inputStream = new FileInputStream(file); byte[] b = new byte[1024]; int len = 0; while ((len = inputStream.read(b)) != -1) { logger.info(new String(b, 0, len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(inputStream); } } private static void inputStream2Reader(File file) { try { inputStream = new FileInputStream(file); reader = new InputStreamReader(inputStream, "UTF-8"); char[] cbuf = new char[1024]; int len = 0; while ((len = reader.read(cbuf)) != -1) { logger.info(new String(cbuf, 0, len)); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(inputStream); closeIO(reader); } } private static void inputStream2BufferedReader(File file) { try { inputStream = new FileInputStream(file); reader = new InputStreamReader(inputStream, "UTF-8"); bufferedReader = new BufferedReader(reader); String string = ""; while ((string = bufferedReader.readLine()) != null) { logger.info(string); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(bufferedReader); closeIO(reader); closeIO(inputStream); } } }
package com.hotfey.demo; import static com.hotfey.util.IOUtil.closeIO; import static com.hotfey.util.IOUtil.createNewFileByFile; import java.io.*; public class OutputDemo { private static Reader reader; private static Writer writer; private static InputStream inputStream; private static OutputStream outputStream; private static BufferedReader bufferedReader; private static BufferedWriter bufferedWriter; public static void main(String[] args) { String pathname = "D:/test.txt"; File inputFile = new File(pathname); File outputfile = createNewFileByFile(inputFile); inputStream2OutputStream(inputFile, outputfile); inputStream2Writer(inputFile, outputfile); inputStream2BufferedWriter(inputFile, outputfile); } private static void inputStream2OutputStream(File inputFile, File outputfile) { try { inputStream = new FileInputStream(inputFile); outputStream = new FileOutputStream(outputfile); byte[] b = new byte[1024]; int len = 0; while ((len = inputStream.read(b)) != -1) { outputStream.write(b, 0, len); outputStream.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(outputStream); closeIO(inputStream); } } private static void inputStream2Writer(File inputFile, File outputfile) { try { inputStream = new FileInputStream(inputFile); reader = new InputStreamReader(inputStream, "UTF-8"); outputStream = new FileOutputStream(outputfile); writer = new OutputStreamWriter(outputStream, "UTF-8"); char[] cbuf = new char[1024]; int len = 0; while ((len = reader.read(cbuf)) != -1) { writer.write(cbuf, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(writer); closeIO(outputStream); closeIO(reader); closeIO(inputStream); } } private static void inputStream2BufferedWriter(File inputFile, File outputfile) { try { inputStream = new FileInputStream(inputFile); reader = new InputStreamReader(inputStream, "UTF-8"); bufferedReader = new BufferedReader(reader); outputStream = new FileOutputStream(outputfile); writer = new OutputStreamWriter(outputStream, "UTF-8"); bufferedWriter = new BufferedWriter(writer); String string = ""; while ((string = bufferedReader.readLine()) != null) { bufferedWriter.write(string + "\r\n"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(bufferedWriter); closeIO(writer); closeIO(outputStream); closeIO(bufferedReader); closeIO(reader); closeIO(inputStream); } } }
package com.hotfey.util; import java.io.*; public class IOUtil { public static File createNewFileByFile(File file) { String fileName = file.getName(); String newPath = file.getName().substring(0, fileName.indexOf(".")); newPath = file.getPath().replace(fileName, newPath); newPath += File.separator + fileName; File newFile = new File(newPath); File newDirectory = new File(newPath.replace(fileName, "")); if (!newDirectory.exists()) { newDirectory.mkdirs(); } if (!newFile.exists()) { try { newFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } return newFile; } public static void closeIO(Reader reader) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeIO(Writer writer) { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeIO(InputStream inputStream) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeIO(OutputStream outputStream) { try { outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
引用
public abstract int read() throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.
引用
public int read(byte[] b) throws IOException
Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.
If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.
The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.
The read(b) method for class InputStream has the same effect as:
read(b, 0, b.length)
Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.
NullPointerException - if b is null.
See Also:
read(byte[], int, int)
引用
public int read(byte[] b, int off, int len) throws IOException
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.
This method blocks until input data is available, end of file is detected, or an exception is thrown.
If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.
The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.
In every case, elements b[0] through b[off] and elements b[off+len] through b[b.length-1] are unaffected.
The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method. If any subsequent call to read() results in a IOException, the exception is caught and treated as if it were end of file; the bytes read up to that point are stored into b and the number of bytes read before the exception occurred is returned. The default implementation of this method blocks until the requested amount of input data len has been read, end of file is detected, or an exception is thrown. Subclasses are encouraged to provide a more efficient implementation of this method.
Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
NullPointerException - If b is null.
IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off
See Also:
read()
引用
public String readLine() throws IOException
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs
See Also:
Files.readAllLines(java.nio.file.Path, java.nio.charset.Charset)
相关推荐
`JavaIODemo-master.zip`是一个包含Java I/O示例代码的项目,可能是为了帮助开发者理解和实践Java I/O的各种功能。在这个项目中,我们可以期待找到不同类型的I/O操作示例,例如读写文件、流的使用、缓冲区技术、转换...
在“JavaIODemo”这个示例中,可能会包含以上提到的一些或全部Java IO操作的代码实例,通过这些实例,我们可以学习如何在实际项目中应用Java IO API。实践是掌握Java IO的最佳方式,通过对这些示例的分析和运行,...
在"JavaIODemo"这个练习中,我们可能会看到以下内容: 1. 流的创建与使用:演示如何创建输入流和输出流对象,例如从文件读取数据并写入到另一个文件。 2. 缓冲技术:使用BufferedReader和BufferedWriter实现高效的...
这个"Socket.IO demo程序"显然是一个用于演示如何在Java项目中使用Socket.IO的实例。 首先,让我们深入了解Socket.IO的核心概念。Socket.IO的目标是提供一个跨平台、易于使用的API,使得开发者能够在各种浏览器和...
在Java编程语言中,输入/输出(IO)流是一组用于读取和写入数据的类,它们构成了Java IO API的基础。本示例是关于如何使用Java中的IO流进行实际操作的演示,主要包括字节流和字符流的使用。字节流处理的是原始的八位...
Socket IO Demo是一个示例项目,主要展示了如何在编程中使用Socket和IO技术。Socket是网络编程中的基础组件,用于在不同计算机之间建立通信链接,而IO(Input/Output)则是处理数据输入和输出的关键部分。在这个demo...
Java Socket 操作 Demo import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.BufferedReader; import ...
4. **IO/NIO**:了解Java如何进行输入输出操作,以及非阻塞I/O的使用。 5. **多线程编程**:学习线程的创建、同步、锁机制等,以及ExecutorService和并发工具类的应用。 6. **网络编程**:如Socket编程,...
在这个"socket.io java demo"中,我们重点讨论的是如何在Java环境下使用Socket.IO。 首先,Socket.IO的主要目标是提供一种在Web应用中实现WebSocket协议的替代方案,因为WebSocket在某些旧的或者不完全支持的浏览器...
通过理解和实践这些Demo,学习者可以逐步熟悉Java的语法和编程思想,为进一步深入学习Java的高级特性,如异常处理、多线程、集合框架、IO流、网络编程等打下坚实基础。在实践中不断探索和练习,是掌握任何编程语言的...
但可以通过JNI(Java Native Interface)调用操作系统API,或者使用第三方库如`org.apache.commons.io.FileUtils`来实现。例如,你可以使用`File`类获取特定目录的大小: ```java File dir = new File("/path/to/...
### Java.io包详解 #### 一、概述 Java.io包是Java编程语言中非常重要的一个标准库,它提供了丰富的类和接口,支持多种输入/输出流的处理方式,包括文件I/O、网络通信等场景。Java.io包的核心设计思想是以流...
在这个"grpc java版本demo"中,我们将探讨如何使用 Java 实现 GRPC 的客户端和服务器。 1. **Protocol Buffers (protobuf)**: protobuf 是一种轻量级的数据序列化协议,用于结构化数据的编码和解码。它支持多种编程...
在这个“java 串口通信 rxtx demo”中,我们将深入探讨如何使用RXTX库来实现Java串口通信。 首先,让我们了解串口通信的基础知识。串口通信是一种点对点的数据传输方式,通常用于设备之间的短距离通信,如打印机、...
Java的IO流是Java编程语言中的重要组成部分,它主要用于数据的输入和输出操作。在Java中,IO流被设计为处理任何类型的数据,包括字符、字节甚至对象。本练习旨在帮助初学者理解和掌握Java IO流的基础知识。 一、IO...
通过这个“JavaIODemo”示例,开发者可以学习到如何在Android环境下高效、安全地进行文件操作、网络通信以及数据序列化等I/O任务。这有助于提升应用的性能和用户体验,同时降低资源消耗。在实际开发中,理解并熟练...
JavaDemo是针对初学者和进阶者的一个编程实践项目,主要使用Java语言编写。这个项目源自阿里云课堂的学习课程,旨在帮助学员通过实际操作来巩固和深化Java编程的知识点。在"JavaDemo.txt"这个文本文件中,我们可以...
此外,Java的异常处理机制、垃圾回收机制以及IO流等也是开发者必须掌握的关键知识点。 在算法方面,Java提供了丰富的数据结构和算法实现,如排序算法(冒泡排序、快速排序、归并排序)、查找算法(二分查找、哈希...
Java NIO(New IO)是Java 1.4版本引入的一个新模块,它提供了一种不同于传统IO(基于字节流和字符流)的I/O操作方式。传统的IO模型是阻塞式的,而NIO的核心特点是非阻塞,这使得在处理大量并发I/O请求时更为高效。...
在这个"java-demo"项目中,我们可以深入学习Java技术,特别是关于多线程、IO流以及两种不同的IO模型——阻塞IO(BIO)和非阻塞IO。此外,还涉及到Netty框架的应用,这是一个高性能、异步事件驱动的网络应用框架,常...