java filedescriptor_Java FileDescriptor sync()方法

Java FileDescriptor sync()方法

java.io.File.sync() 方法强制所有系统缓冲区与基础设备同步。

1 语法

public void sync()

2 参数

3 返回值

4 示例

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* java.io.File.sync()方法的例子

*/

import java.io.FileDescriptor;

import java.io.FileInputStream;

import java.io.FileOutputStream;

public class Demo {

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

FileInputStream fis = null;

FileOutputStream fos = null;

FileDescriptor fd = null;

byte[] b = {65,66,67,68,69,70};

try {

fos = new FileOutputStream("c:/java test.txt");

fd = fos.getFD();

// writes byte to file output stream

fos.write(b);

// flush data from the stream into the buffer

fos.flush();

// confirms data to be written to the disk

fd.sync();

// create input stream

fis = new FileInputStream("c:/java test.txt");

int value = 0;

// for every available bytes

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

// converts bytes to char

char c = (char)value;

// prints char

System.out.print(c);

}

// print

System.out.print("\nSync() successfully executed!!");

} catch(Exception e) {

// if any error occurs

e.printStackTrace();

} finally {

// releases system resources

if(fos!=null)

fos.close();

if(fis!=null)

fis.close();

}

}

}

输出结果为:

ABCDEF

Sync() successfully executed!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
libusb_get_string_descriptor_ascii()函数用于获取设备的字符串描述符。它的函数原型如下: ```c int libusb_get_string_descriptor_ascii( libusb_device_handle *dev_handle, uint8_t desc_index, unsigned char *data, int length ); ``` 参数说明: - `dev_handle`:libusb设备句柄,可以通过libusb_open()函数打开设备获取。 - `desc_index`:字符串描述符的索引,通常是设备描述符中的字符串描述符索引字段(例如,`iManufacturer`、`iProduct`、`iSerialNumber`)。 - `data`:用于存储字符串描述符数据的缓冲区。 - `length`:缓冲区的长度。 该函数将指定索引的字符串描述符数据复制到提供的缓冲区中,并返回实际复制的字节数。如果成功,返回值为0;如果失败,返回负数错误代码。 以下是一个使用libusb_get_string_descriptor_ascii()函数获取设备制造商字符串描述符的示例代码: ```c #include <stdio.h> #include <libusb-1.0/libusb.h> int main() { libusb_context *ctx = NULL; libusb_device_handle *dev_handle = NULL; int r; r = libusb_init(&ctx); if (r < 0) { printf("Failed to initialize libusb\n"); return 1; } dev_handle = libusb_open_device_with_vid_pid(ctx, vendor_id, product_id); if (dev_handle == NULL) { printf("Failed to open device\n"); libusb_exit(ctx); return 1; } unsigned char manufacturer[256]; r = libusb_get_string_descriptor_ascii(dev_handle, desc_index, manufacturer, sizeof(manufacturer)); if (r < 0) { printf("Failed to get manufacturer string descriptor\n"); libusb_close(dev_handle); libusb_exit(ctx); return 1; } printf("Manufacturer: %s\n", manufacturer); libusb_close(dev_handle); libusb_exit(ctx); return 0; } ``` 在上述示例中,您需要将`vendor_id`和`product_id`替换为您要打开的设备的实际供应商ID和产品ID。另外,您还需要将`desc_index`替换为设备描述符中制造商字符串描述符的索引。 希望这能帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值