用TS写一个读写文件的程序DEMO
时间: 2023-09-28 20:03:51 浏览: 52
好的,以下是用 TypeScript 写的读写文件的 DEMO:
```typescript
import fs from 'fs';
// 读取文件
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
// 写入文件
fs.writeFile('example.txt', 'Hello, world!', (err) => {
if (err) {
console.error(err);
return;
}
console.log('文件写入成功!');
});
```
在这段代码中,我们使用 Node.js 内置的 `fs` 模块来读写文件。其中,`readFile` 方法用于读取文件,`writeFile` 方法用于写入文件。第一个参数是文件路径,第二个参数是文件编码方式(在这里我们使用 `utf8`),第三个参数是一个回调函数,用于处理读取或写入文件的结果。如果读取或写入文件出现错误,回调函数的第一个参数会是一个错误对象,否则第二个参数会是读取到的文件内容或写入成功的提示信息。
相关问题
pca8565ts程序读写时钟程序
PCA8565TS是一种实时时钟芯片,可以通过I2C接口进行读写。下面是一个简单的示例代码,演示如何使用Arduino板与PCA8565TS进行通信:
```c++
#include <Wire.h>
#define RTC_ADDRESS 0x51
byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}
byte decToBcd(byte val) {
return ( (val/10*16) + (val%10) );
}
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(RTC_ADDRESS);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(RTC_ADDRESS, 3);
byte sec = bcdToDec(Wire.read() & 0x7f); // seconds
byte min = bcdToDec(Wire.read()); // minutes
byte hour = bcdToDec(Wire.read() & 0x3f); // hours
Serial.print(hour);
Serial.print(":");
if (min < 10) {
Serial.print("0");
}
Serial.print(min);
Serial.print(":");
if (sec < 10) {
Serial.print("0");
}
Serial.println(sec);
delay(1000);
}
```
这个程序使用了Wire库来与PCA8565TS进行I2C通信。在setup函数中,我们初始化了Wire库和串行通信。在loop函数中,我们首先发送一个读指令,然后请求3个字节的数据。接着,我们将读取到的BCD码转换为十进制数字,并且打印出当前的时间。最后,我们等待1秒钟再次读取时间。
要写入时间到PCA8565TS,我们可以使用类似的代码:
```c++
void setTime(byte hour, byte min, byte sec) {
Wire.beginTransmission(RTC_ADDRESS);
Wire.write(0x02); // seconds register
Wire.write(decToBcd(sec));
Wire.write(decToBcd(min));
Wire.write(decToBcd(hour));
Wire.endTransmission();
}
```
这个函数将小时、分钟和秒作为参数,并将它们转换为BCD码后写入PCA8565TS的寄存器中。在调用这个函数之前,我们需要确保我们已经初始化了Wire库和PCA8565TS。
vue3+ts读写本地文件
在Vue3+TypeScript中,可以使用HTML5的File API来读写本地文件。具体步骤如下:
1. 在template中添加一个input元素,设置type为file,用于选择本地文件。
2. 在Vue组件中定义一个方法,用于处理文件读取。该方法需要接收一个event参数,通过event.target.files获取到选择的文件。
3. 使用FileReader对象读取文件内容,可以使用readAsText()方法将文件内容读取为文本格式。
4. 在读取完成后,将文件内容保存到Vue组件的data属性中,即可在template中显示文件内容。
示例代码如下:
```html
<template>
<div>
<input type="file" @change="handleFileChange">
<div>{{ fileContent }}</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
data() {
return {
fileContent: '',
};
},
methods: {
handleFileChange(event: Event) {
const file = (event.target as HTMLInputElement).files?.[0]; if (file) {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
this.fileContent = reader.result as string;
};
}
},
},
});
</script>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)