search
HomeJavajavaTutorialHow to open and write files using Java language

1. Open the file in Java

This example uses the read(buffer) method of FileInputStream to read 512 bytes from the source program file OpenFile.java each time and store In the buffer buffer, the string newString(buffer) constructed from the value in the buffer is displayed on the screen. The program is as follows:

importjava.io.*;  publicclassOpenFile  {  publicstaticvoidmain(Stringargs[])throwsIOException  {  try  {//创建文件输入流对象  FileInputStreamrf=newFileInputStream("OpenFile.java");  intn=512;  bytebuffer[]=newbyte[n];  while((rf.read(buffer,0,n)!=-1)&&(n>0))//读取输入流  {  System.out.print(newString(buffer));  }  System.out.println();  rf.close();//关闭输入流  }  catch(IOExceptionioe)  {  System.out.println(ioe);  }  catch(Exceptione)  {  System.out.println(e);  }  }  }

2, Java language writing file

This example uses System.in.read(buffer) to input a line from the keyboard Characters are stored in the buffer buffer, and then the write(buffer) method of FileOutStream is used to write the contents of the buffer to the file Write1.txt. The procedure is as follows:

importjava.io.*;  publicclassWrite1  {  publicstaticvoidmain(Stringargs[])  {  try  {  System.out.print("Input:");  intcount,n=512;  bytebuffer[]=newbyte[n];  count=System.in.read(buffer);//读取标准输入流  FileOutputStreamwf=newFileOutputStream("Write1.txt");  //创建文件输出流对象  wf.write(buffer,0,count);//写入输出流  wf.close();//关闭输出流  System.out.println("SavetoWrite1.txt!");  }  catch(IOExceptionioe)  {  System.out.println(ioe);  }  catch(Exceptione)  {  System.out.println(e);  }  }  }

The above is the detailed content of How to open and write files using Java language. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment