java基础:12.4 字符流 Reader Writer

 

字符流

上一节学会了按字节进行操作的–字符流操作方法,InputStreamOutputStream

本节学习专门用于字符的形式读取和写入数据的方法
Reader 字符输入流
Writer 字符输出流

 

package stream;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test_Reader_Writer {
	
		//读取文件
	public static void readfile(File f) {
		try (FileReader fr = new FileReader(f))
		{
			char[] all = new char[(int)f.length()];
			fr.read(all);
			for(char b:all)
				System.out.print(b);
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	
	}
	
	//写文件
	public static void writefile(File f) {
		try(FileWriter fr = new FileWriter(f)){
			String a = "today.";
			char []s2c = a.toCharArray();
			fr.write(s2c);
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}		
	
	//加密文件
	public static void encodeFile(File encodingFile, File encodedFile) {	
		if(encodingFile.length()==0)
			return;
		char[] encoding = new char[(int)encodingFile.length()];
		char[] encoded = new char[(int)encodingFile.length()];
		
		try{ 
			FileReader fr = new FileReader(encodingFile);
			fr.read(encoding);
			fr.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		
		//processing
		System.out.print("未加密的内容:");
		for(char b:encoding)
			System.out.print(b);
		System.out.println();
		
		for(int i=0; i<encodingFile.length();i++) {
			short ASCII = (short)encoding[i];
			
			if( '0'<= encoding[i] &&  encoding[i] <= '9') {
				if(encoded[i] == '0') encoded[i] = '9';
				else encoded[i] = (char)(ASCII+1);
			}
			
			else 
				if(('a'<= encoding[i] && encoding[i] <= 'z') || 
					('A'<= encoding[i] && encoding[i] <= 'Z') )

					if(encoded[i] =='a')  encoded[i] =  (char)(ASCII+25);
					else  
						if(encoded[i] =='A') encoded[i] = 'Z';
						else encoded[i]= (char)(ASCII-1);				
				else encoded[i]= encoding[i];			
	
			}
		//output
		try {
		FileWriter fw = new FileWriter(encodedFile);
		fw.write(encoded);
		fw.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		System.out.print("解密后的内容:");
        System.out.println(new String(encoded));			
	}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
  
public class Main {
  
    public static void main(String[] args) {
       // int eachSize = 100 * 1024;    // 100k
       // File srcFile = new File("f:/testfile/0321.pptx");
       // OperateFile.splitFile(srcFile, eachSize);
      //  OperateFile.mergeFile(8,eachSize);
        
        File f = new File("f:/testfile/file.txt");
        Test_Reader_Writer.readfile(f);
        Test_Reader_Writer.writefile(f);
        
        File file_textConding = new File("f:/testfile/coding.txt");
        File file_textConding_out = new File("f:/testfile/coded.txt");
        Test_Reader_Writer.encodeFile(file_textConding, file_textConding_out);
        
    }    
}

转载于:https://www.cnblogs.com/l20902/p/10610885.html

在安装支持CUDA 12.4版本的PyTorch之前,请确保你的系统环境满足以下条件: 1. 已经安装了CUDA 12.4版本的对应驱动程序。 2. 你的系统满足PyTorch的最低硬件要求。 3. 如果你是使用的是Linux系统,需要安装Python和pip。 安装PyTorch的步骤通常如下: 1. 访问PyTorch官方网站的获取页面:[https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/)。 2. 根据你的系统配置选择正确的安装命令。在选择时,需要指定CUDA版本、包管理器(如conda或pip)、Python版本和是否需要预编译的二进制包(通常选择cu124表示CUDA 12.4)。 3. 例如,如果你使用的是conda,在Linux系统上且希望使用Python 3.8,安装命令可能如下所示: ``` conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia ``` 4. 如果你希望使用pip安装,相应的命令可能如下所示: ``` pip install torch torchvision torchaudio --pre --extra-index-url https://download.pytorch.org/whl/cu124 ``` 5. 运行安装命令后,系统将自动下载并安装指定版本的PyTorch及其依赖。 6. 安装完成后,可以通过运行Python并输入以下代码来验证安装是否成功: ```python import torch print(torch.__version__) print(torch.cuda.is_available()) ``` 如果安装成功,它将输出PyTorch的版本号,并确认CUDA可用性。 请根据你的系统情况选择正确的安装方式,并确保CUDA版本和PyTorch版本兼容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值