簡體   English   中英

將字符串作為文件存儲在 java 中的數據庫中

[英]Storing a String as a file in database in java

我想將 String 變量的內容轉換為(我不知道正確的術語)文件並將其存儲在我的數據庫中。 我實際上不需要該文件,只需要將其存儲在數據庫中,以便稍后在前端下載。

我希望操作在后端自動發生(Rest API 將 String 變量轉換為擴展名為 .pem 的文件並將其存儲在數據庫中)。 是否可以? 任何幫助表示贊賞。

也許我的想法是您想將一些數據存儲在文件中。如果有誤,請通知我。 您需要為此導入一些包:-

import java.io.File;
import java.io.Scanner;
import java.io.FileWriter;
import java.util.Arrays;

使用以下代碼:-

public void writetoFile(String pathtofile, String text)
{
try{

File file = new File(pathtofile);
FileWriter fw = new FileWriter(file);
fw.write(text);
fw.close();
}
catch(IOException e){
System.out.println(e);
}
}

//If you want to read content from the file, you can get all lines in an array or other method you wish.

public String [] readfromFile(String path){
try{
File file = new File(path);
Scanner scan = new Scanner(file);
String [] out = new String[1];
int i=0;
while(scan.hasNextLine()){
out[i]=scan.nextLine();
i++;
out = Arrays.copyOf(out,out.legnth+1);
}
}catch(IOException e){
System.out.println(e);
}
return out;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM