「java数据写到磁盘」java顺序写磁盘

博主:adminadmin 2022-11-27 08:02:08 434

今天给各位分享java数据写到磁盘的知识,其中也会对java顺序写磁盘进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中怎么吧文件写入磁盘?

java写文件有两个类可用,参考下面的例子。

FileOutputStream:

FileOutputStream fos=new FileOutputStream("my.txt"); // 创建输出类

fos.write("我的内容".getBytes());                    // 写入数据,只能是byte

fos.clse();                                          // 关闭文件

FileWriter:

FileWriter fw=new FileWriter("my.txt"); // 创建输出类

fw.write("我的内容");                   // 写入数据,可以直接写字符串

fw.close();                             // 关闭文件

java里数据怎么保存到硬盘或TXT文件里去

Java是通过使用I/O文件操作类,创建输入输出流,将数据保存在指定的路径下的文件里面。

示例代码:

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

public class WriteFileTest {

public static void main(String[] args) {

FileOutputStream fop = null;

File file;

String content = "This is the text content";

try {

file = new File("D:/test.txt");//初始化file

fop = new FileOutputStream(file);//初始化输出流

// 若文件不存在,则创建它

if (!file.exists()) {

file.createNewFile();

}

// 获取字节的内容数组

byte[] contentInBytes = content.getBytes();

fop.write(contentInBytes);//写出到指定路径文件中字节的内容数组

fop.flush();

fop.close();

System.out.println("Done");

} catch (IOException e) { //捕捉异常

e.printStackTrace();

} finally {

try {

if (fop != null) {

fop.close();

}

} catch (IOException e) { //捕捉异常

e.printStackTrace();

}

}

}

}

如何用java写出用最快速度向硬盘写入文件

java写文件有两个类可用,参考下面的例子。

FileOutputStream:

FileOutputStream fos=new FileOutputStream("my.txt"); // 创建输出类

fos.write("我的内容".getBytes()); // 写入数据,只能是byte

fos.clse(); // 关闭文件

FileWriter:

FileWriter fw=new FileWriter("my.txt"); // 创建输出类

fw.write("我的内容"); // 写入数据,可以直接写字符串

fw.close(); // 关闭文件

java怎么往本地磁盘上写文件

这是一个很有用的文件工具类,你可以把他存起来以后使用。

她应该可以满足你日常文件的基本功能啦!写文件,读文件,复制文件,复制文件夹等。

希望是你想要的.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

/**

*

* p

* Title: 文件处理工具类

* /p

* p

* Description:实现文件的简单处理,复制文件、目录等

* /p

* p

* Copyright: Copyright (c) 2005

* /p

* p

* Company:

* /p

*

* @author 天一

* @version 1.0

*/

public class FileUtil {

/**

* 复制目录下的文件(不包括该目录)到指定目录,会连同子目录一起复制过去。

*

* @param targetFile

* @param path

*/

public static void copyFileFromDir(String targetDir, String path) {

File file = new File(path);

createFile(targetDir, false);

if (file.isDirectory()) {

copyFileToDir(targetDir, listFile(file));

}

}

/**

* 复制目录下的文件(不包含该目录和子目录,只复制目录下的文件)到指定目录。

*

* @param targetDir

* @param path

*/

public static void copyFileOnly(String targetDir, String path) {

File file = new File(path);

File targetFile = new File(targetDir);

if (file.isDirectory()) {

File[] files = file.listFiles();

for (File subFile : files) {

if (subFile.isFile()) {

copyFile(targetFile, subFile);

}

}

}

}

/**

* 复制目录到指定目录。targetDir是目标目录,path是源目录。

* 该方法会将path以及path下的文件和子目录全部复制到目标目录

*

* @param targetDir

* @param path

*/

public static void copyDir(String targetDir, String path) {

File targetFile = new File(targetDir);

createFile(targetFile, false);

File file = new File(path);

if (targetFile.isDirectory() file.isDirectory()) {

copyFileToDir(targetFile.getAbsolutePath() + "/" + file.getName(),

listFile(file));

}

}

/**

* 复制一组文件到指定目录。targetDir是目标目录,filePath是需要复制的文件路径

*

* @param targetDir

* @param filePath

*/

public static void copyFileToDir(String targetDir, String... filePath) {

if (targetDir == null || "".equals(targetDir)) {

System.out.println("参数错误,目标路径不能为空");

return;

}

File targetFile = new File(targetDir);

if (!targetFile.exists()) {

targetFile.mkdir();

} else {

if (!targetFile.isDirectory()) {

System.out.println("参数错误,目标路径指向的不是一个目录!");

return;

}

}

for (String path : filePath) {

File file = new File(path);

if (file.isDirectory()) {

copyFileToDir(targetDir + "/" + file.getName(), listFile(file));

} else {

copyFileToDir(targetDir, file, "");

}

}

}

/**

* 复制文件到指定目录。targetDir是目标目录,file是源文件名,newName是重命名的名字。

*

* @param targetFile

* @param file

* @param newName

*/

public static void copyFileToDir(String targetDir, File file, String newName) {

String newFile = "";

if (newName != null !"".equals(newName)) {

newFile = targetDir + "/" + newName;

} else {

newFile = targetDir + "/" + file.getName();

}

File tFile = new File(newFile);

copyFile(tFile, file);

}

/**

* 复制文件。targetFile为目标文件,file为源文件

*

* @param targetFile

* @param file

*/

public static void copyFile(File targetFile, File file) {

if (targetFile.exists()) {

System.out.println("文件" + targetFile.getAbsolutePath() + "已经存在,跳过该文件!");

return;

} else {

createFile(targetFile, true);

}

System.out.println("复制文件" + file.getAbsolutePath() + "到" + targetFile.getAbsolutePath());

try {

InputStream is = new FileInputStream(file);

FileOutputStream fos = new FileOutputStream(targetFile);

byte[] buffer = new byte[1024];

while (is.read(buffer) != -1) {

fos.write(buffer);

}

is.close();

fos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static String[] listFile(File dir) {

String absolutPath = dir.getAbsolutePath();

String[] paths = dir.list();

String[] files = new String[paths.length];

for (int i = 0; i paths.length; i++) {

files[i] = absolutPath + "/" + paths[i];

}

return files;

}

public static void createFile(String path, boolean isFile) {

createFile(new File(path), isFile);

}

public static void createFile(File file, boolean isFile) {

if (!file.exists()) {

if (!file.getParentFile().exists()) {

createFile(file.getParentFile(), false);

} else {

if (isFile) {

try {

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

} else {

file.mkdir();

}

}

}

}

}

Java怎样把文件写入到客户端的硬盘上

package test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

public class Test {

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

        String source = "hello world!";

        String filePath = "c:\\file.txt";

        String newFilePath = "c:\\" + System.currentTimeMillis() + ".txt";

        

        saveFile1(source);

        saveFile2(filePath, newFilePath);

        

    }

    

    /**

     * 直接写入数据

     * @param source

     * @return

     */

    public static boolean saveFile1(String source){

        byte buf[] = source.getBytes();

        try(FileOutputStream fs = new FileOutputStream("c:\\file.txt")){

            for (int i = 0; i  buf.length; i++) {

                fs.write(buf[i]);

            }

            return true;

        }catch (Exception e) {

            e.printStackTrace();

        }

        return false;

    }

    /**

     * 读取文件另存为

     * @param filePath

     * @param newFilePath

     * @return

     */

    public static boolean saveFile2(String filePath, String newFilePath){

        File file = new File(filePath);

        if (file.exists()  file.isFile()) {

            try (FileInputStream fi = new FileInputStream(file);

                 FileOutputStream fs = new FileOutputStream(newFilePath))

            {

                int buf;

                while ((buf = fi.read()) != -1) {

                    fs.write(buf);

                }

                return true;

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return false;

    }

}

java中如何通过IO流将稀疏数组写入磁盘和从磁盘中读取,整行存,整行取

//写入磁盘

public static void writ(int sparseArr[][]) {

System.out.println("写入磁盘的数据中~~~~~~");

File file = new File("E:\\java\\sparseArr.txt");

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new FileWriter(file));

if (!file.exists()) {

file.createNewFile();

}

StringBuilder allBuilder = new StringBuilder();

for (int[] rows : sparseArr) {

StringBuilder rowBuilder = new StringBuilder();

for (int item : rows) {

rowBuilder.append(item + "\t");

}

allBuilder.append(rowBuilder + "\n");

}

bw.write(String.valueOf(allBuilder));

bw.flush();

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

//读取磁盘

public static int[][] read() {

System.out.println("读取磁盘的数据中~~~~~~");

File file = new File("E:\\java\\sparseArr.txt");

int[][] sparseArr = null;

try {

BufferedReader br = new BufferedReader(new FileReader(file));

if (!file.exists()) {

file.createNewFile();

}

String row = br.readLine();

String[] s = new String(row).split("\t");

sparseArr = new int[Integer.parseInt(s[0])][Integer.parseInt(s[1])];

while ((row = br.readLine()) != null) {

String[] s2 = new String(row).split("\t");

sparseArr[Integer.parseInt(s2[0])][Integer.parseInt(s2[1])] = Integer.parseInt(s2[2]);

}

br.close();

} catch (IOException e) {

e.printStackTrace();

}

return sparseArr;

}

关于java数据写到磁盘和java顺序写磁盘的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。