java将文件写入txt

try {

    File writeName = new File("D:\\data.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件
    if(!writeName.exists()) {
        writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖    }
    FileWriter writer = new FileWriter(writeName);
    BufferedWriter out = new BufferedWriter(writer);
    out.write(data);
    out.flush(); // 把缓存区内容压入文件
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}

try {
        File file = new File("D:\\data.txt");
        if(!file.exists()) {
            file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖        }
        FileOutputStream fos = new FileOutputStream(file,true);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(data);
        bw.newLine();
        bw.flush();
        bw.close();
        osw.close();
        fos.close();
}catch (FileNotFoundException e1) {
    e1.printStackTrace();
} catch (IOException e2) {
    e2.printStackTrace();
}

//将数组写入txt

package org.nms.fast.manage.Utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import org.junit.Test;
import org.nms.fast.common.snmp.OtdrData;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.math.BigDecimal;

/**
 * ganymed的版本选择build210
 *         <dependency>
 *             <groupId>ch.ethz.ganymed</groupId>
 *             <artifactId>ganymed-ssh2</artifactId>
 *             <version>build210</version>
 *         </dependency>
 * @author :maph
 * @version :
 * @date :Created in 2020-03-25 20:41
 * @description:
 * @modified By:
 */
@Component
public class ScpUtil {

    private static String IP = "";
    private static int PORT = 22;
    private static String USER = "gzster";//登录用户名
    private static String PASSWORD = "St@#()69";//生成私钥的密码和登录密码,这两个共用这个密码
    private static Connection connection;
    private static String PRIVATEKEY = "C:\\Users\\mph\\.ssh\\id_rsa";// 本机的私钥文件
    private static boolean usePassword = true;// 使用用户名和密码来进行登录验证。如果为true则通过用户名和密码登录,false则使用rsa免密码登录


    public void setParameter(String snmpdeviceIp) {
        IP = snmpdeviceIp;
        connection = new Connection(IP, PORT);
    }

    /**
     * ssh用户登录验证,使用用户名和密码来认证
     *
     * @param user
     * @param password
     * @return
     */
    public static boolean isAuthedWithPassword(String user, String password) {
        try {
//            connection.requestRemotePortForwarding("172.16.16.1", 22, "172.16.16.15", 22);
            return connection.authentica

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值