Java RandomAccessFile readUTF()方法及示例

RandomAccessFile类readUTF()方法 (RandomAccessFile Class readUTF() method)

  • readUTF() method is available in java.io package.

    readUTF()方法在java.io包中可用。

  • readUTF() method is used to read this RandomAccessFile as a string.

    readUTF()方法用于以字符串形式读取此RandomAccessFile。

  • readUTF() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    readUTF()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • readUTF() method may throw an exception at the time of reading the file as a string.

    readUTF()方法在将文件读取为字符串时可能会引发异常。

    • IOException: This exception may throw an exception while performing input/output operation.IOException :在执行输入/输出操作时,此异常可能会引发异常。
    • EOFException: This exception may throw when the file pointer reaches EOF (End-Of-File) before reading all bytes.EOFException :当文件指针在读取所有字节之前到达EOF(文件末尾)时,可能引发此异常。
    • UTFDataFormatException: This exception may throw when the bytes denote invalid UTF-8 encoding schemes.UTFDataFormatException :当字节表示无效的UTF-8编码方案时,可能引发此异常。

Syntax:

句法:

    public final String readUTF();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is String, it gets Unicode string value.

此方法的返回类型为String ,它获取Unicode字符串值。

Example:

例:

// Java program to demonstrate the example 
// of String readUTF() method of
// RandomAccessFile

import java.io.*;

class RAFReadUTF {
 public static void main(String[] args) throws Exception {
  // Instantiate a random access file
  // object with file name and permissions
  RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");

  // By using writeUTF() method is to 
  // write data to the file
  ra_f.writeUTF("Welcome in Java World!!!");

  // Initially set the file pointer
  // is at 1 for reading the file
  ra_f.seek(0);

  // By using readUTF() method is to 
  // read a data in a string from
  // this file

  //String str = ra_f.readUTF();
  System.out.println("ra_f.readUTF(): " + ra_f.readUTF());

  // By using close() method isto
  // close this stream ran_f
  ra_f.close();
 }
}

Output

输出量

ra_f.readUTF(): Welcome in Java World!!!


翻译自: https://www.includehelp.com/java/randomaccessfile-readutf-method-with-example.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.io.*; import java.net.*; import java.util.*; public class Server { public static void main(String args[]){ try { ServerSocket server = new ServerSocket(8888); System.out.println("服务器在监听8888端口..."); while (true) { Socket socket = server.accept(); new ReceFile(socket).start(); } } catch (Exception e) { e.printStackTrace(); } } } //服务器接收文件线程 class ReceFile extends Thread { private Socket socket; public ReceFile(Socket socket){ this.socket = socket; } public void run(){ InputStream is = null; OutputStream os = null; DataInputStream dis = null; InetAddress address = null; String ip = ""; try { //获得输入流 is = socket.getInputStream(); //获取连接此输入流的地址 address = socket.getInetAddress(); //返回ip ip = address.getHostAddress(); dis = new DataInputStream(is); //读取上传过过来的文件名 String fileName = dis.readUTF(); System.out.println("服务器接收到自"+ip+"的"+fileName+"文件"); //将接收到的文件保存到指定的目录 os = new FileOutputStream("File/"+fileName); int len = 0; byte[] b = new byte[1024]; while (true) { len = is.read(b); if (len==-1) { break; } os.write(b,0,len); os.flush(); } } catch (Exception e) { e.printStackTrace(); } finally{ try { if (is!=null) { is.close(); } if (dis!=null) { dis.close(); } if (os!=null) { os.close(); } } catch (Exception ex) { ex.printStackTrace(); } } } } import java.io.*; import java.net.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.roy.*; public class Client extends JFrame implements ActionListener { private JButton selbutton,upbutton; private JPanel jp; private String path; public Client(){ super("文件上传"); this.setSize(250,250); this.setLocation(300,400); init(); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public void init(){ selbutton = new JButton("选择文件"); upbutton = new JButton("上 传"); jp = new JPanel(); selbutton.addActionListener(this); upbutton.addActionListener(this); jp.add(selbutton); jp.add(upbutton); this.add(jp); } public void actionPerformed(ActionEvent e){ if(e.getSource()==selbutt

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值