java中获取系统的换行符和文件系统分隔符

楔子

java操作文件是涉及到换行符。apache.commons.io.IOUtils 中会自动获取换行符。虽然不清楚 IO jar是怎么实现的。

IOUtils中代码

    public static final String LINE_SEPARATOR_UNIX = "\n";
    /**
     * The Windows line separator string.
     */
    public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
    /**
     * The system line separator string.
     */
    public static final String LINE_SEPARATOR;

    static {
        // avoid security issues
        StringBuilderWriter buf = new StringBuilderWriter(4);
        PrintWriter out = new PrintWriter(buf);
        out.println();
        LINE_SEPARATOR = buf.toString();
        out.close();
    }

测试

    public static void main(String[] args) {
        ////////////////////////////////////////////

        char DIR_SEPARATOR = File.separatorChar;
        System.out.println("文件系统分隔符"+DIR_SEPARATOR);
        /////////////////////////////////////////////
        StringBuilderWriter buf = new StringBuilderWriter(4);
        PrintWriter out = new PrintWriter(buf);
        out.println();
        String LINE_SEPARATOR = buf.toString();
        if (IOUtils.LINE_SEPARATOR_UNIX.equals(LINE_SEPARATOR)) {
            System.out.println("Linux 换行符");

        } else if (IOUtils.LINE_SEPARATOR_WINDOWS.equals(LINE_SEPARATOR)) {
            System.out.println("window  换行符");
        }
        out.close();
    }

这里写图片描述

以下是一个读取 MySQL 数据库表数据并将其写入 FTP 服务器文件系统Java 代码示例。代码使用了 MySQL 的官方 JDBC 驱动程序,以及 Apache Commons Net 库来处理 FTP 操作。 ```java import java.io.*; import java.sql.*; import org.apache.commons.net.ftp.*; public class MysqlToFtp { public static void main(String[] args) { // MySQL 数据库连接参数 String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "myusername"; String password = "mypassword"; String tableName = "mytable"; // FTP 服务器连接参数 String ftpServer = "ftp.example.com"; String ftpUser = "ftpuser"; String ftpPassword = "ftppassword"; String ftpPath = "/wj/"; // 获取当前日期 java.util.Date today = new java.util.Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd"); String dateStr = sdf.format(today); // 文件名 String fileName = dateStr + ".txt"; try { // 连接 MySQL 数据库 Connection conn = DriverManager.getConnection(url, user, password); // 查询数据库表数据 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName); // 创建输出流 FileWriter fw = new FileWriter(fileName); // 写入表头 ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { fw.write(metaData.getColumnLabel(i)); if (i < columnCount) { fw.write(","); } } fw.write("\r\n"); // 写入数据 while (rs.next()) { for (int i = 1; i <= columnCount; i++) { fw.write(rs.getString(i)); if (i < columnCount) { fw.write(","); } } fw.write("\r\n"); } // 关闭输出流和数据库连接 fw.close(); conn.close(); // 上传文件到 FTP 服务器 FTPClient ftpClient = new FTPClient(); ftpClient.connect(ftpServer); ftpClient.login(ftpUser, ftpPassword); ftpClient.changeWorkingDirectory(ftpPath); FileInputStream fis = new FileInputStream(fileName); ftpClient.storeFile(fileName, fis); ftpClient.logout(); ftpClient.disconnect(); // 删除本地文件 File file = new File(fileName); file.delete(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在代码,您需要替换以下值以使其适应您的环境: - `url`:MySQL 数据库连接字符串,需要包括数据库名称和端口号。 - `user` 和 `password`:MySQL 数据库登录凭据。 - `tableName`:要读取数据的 MySQL 数据表名称。 - `ftpServer`:FTP 服务器主机名或 IP 地址。 - `ftpUser` 和 `ftpPassword`:FTP 服务器登录凭据。 - `ftpPath`:要将文件上传到的 FTP 服务器目录。 此代码还假设您已经在本地安装了 MySQL 的 JDBC 驱动程序和 Apache Commons Net 库,并且已将它们添加到您的项目依赖项
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值