printwriter报错_java输出print和println

printwriter报错_java输出print和println我是一位长期读者,但是第一次写作.我目前正在尝试在代码库中使用AspectJ实现一个记录器.AspectJ似乎运行良好,但我遇到了非常奇怪的Java错误.我是一个长期的C和.Net开发人员,他仍然适应Java的世界,所以如果这是一个愚蠢的问题我很抱歉.我的代码试图捕获异常,并将相关信息记录到文本文件中.陷阱工作正常,但我注意到,当我部署时,我没有得到任何数据.我在Java反编译器中打开了我的类文…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

我是一位长期读者,但是第一次写作.

我目前正在尝试在代码库中使用AspectJ实现一个记录器. AspectJ似乎运行良好,但我遇到了非常奇怪的Java错误.我是一个长期的C和.Net开发人员,他仍然适应Java的世界,所以如果这是一个愚蠢的问题我很抱歉.

我的代码试图捕获异常,并将相关信息记录到文本文件中.陷阱工作正常,但我注意到,当我部署时,我没有得到任何数据.我在Java反编译器中打开了我的类文件,并注意到PrintWriter似乎正在生成错误.我从来没有见过像这样的问题,所以我希望你可以有任何见解.

package mil.uscg.c3cen.vic.aspect;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.JoinPoint;

@Aspect

public class LoggingAspect

{

private final String LOG_FILE = “aspectLog.txt”;

private final File file = new File(LOG_FILE);

private LoggingAspect()

{

}

private void logException(String msg)

{

try

{

if(!file.exists())

file.createNewFile();

}

catch(IOException e)

{

}

try (FileWriter fw = new FileWriter(file);

BufferedWriter bw = new BufferedWriter(fw);

PrintWriter pw = new PrintWriter(bw))

{

pw.println(msg);

}

catch(IOException e)

{

}

}

private String getSimpleFunctionInfo(String className, String function, Object[] args)

{

StringBuilder builder = new StringBuilder();

builder.append(“. Method: “);

builder.append(className);

builder.append(“.”);

builder.append(function);

if(args.length == 0)

{

builder.append(“()”);

return builder.toString();

}

builder.append(“(“);

for(Object o : args)

{

builder.append(o.toString());

builder.append(“,”);

}

// Replace the comma for the last param with a closing parenthesis

int len = builder.length();

builder.replace(len -1, len, “)”);

return builder.toString();

}

// Returns a formatted exception. “Exception.ErrorMessage”

private String getSimpleExceptionInfo(String name, String msg)

{

StringBuilder builder = new StringBuilder();

builder.append(“Exception caught: “);

builder.append(name);

builder.append(“. Message: “);

builder.append(msg);

return builder.toString();

}

@AfterThrowing(pointcut = “execution(* mil.uscg.c3cen.*.*.*(..)) “

//+ “&& !within(mil.uscg.c3cen.vic.aspect.*) “

, throwing = “excep”)

public void afterThrowing(JoinPoint jp, Throwable excep) throws Throwable

{

String ex = getSimpleExceptionInfo(excep.getClass().getSimpleName(),

excep.getMessage());

String name = getSimpleFunctionInfo(jp.getSignature().getDeclaringType().getSimpleName(),

jp.getSignature().getName(),

jp.getArgs());

StringBuilder builder = new StringBuilder();

builder.append(ex);

builder.append(name);

logException(builder.toString());

}

}

除了函数logException之外,一切看起来都像在类文件中一样.

/* Error */

private void logException(String msg)

{

// Byte code:

// 0: aload_0

// 1: getfield 25 mil/uscg/c3cen/vic/aspect/LoggingAspect:file Ljava/io/File;

// 4: invokevirtual 32 java/io/File:exists ()Z

// 7: ifne +15 -> 22

// 10: aload_0

// 11: getfield 25 mil/uscg/c3cen/vic/aspect/LoggingAspect:file Ljava/io/File;

// 14: invokevirtual 36 java/io/File:createNewFile ()Z

// 17: pop

// 18: goto +4 -> 22

// 21: pop

// 22: aconst_null

// 23: astore_2

// 24: aconst_null

// 25: astore_3

// 26: new 39 java/io/FileWriter

// 29: dup

// 30: aload_0

// 31: getfield 25 mil/uscg/c3cen/vic/aspect/LoggingAspect:file Ljava/io/File;

// 34: invokespecial 41 java/io/FileWriter: (Ljava/io/File;)V

// 37: astore 4

// 39: new 44 java/io/BufferedWriter

// 42: dup

// 43: aload 4

// 45: invokespecial 46 java/io/BufferedWriter: (Ljava/io/Writer;)V

// 48: astore 5

// 50: new 49 java/io/PrintWriter

// 53: dup

// 54: aload 5

// 56: invokespecial 51 java/io/PrintWriter: (Ljava/io/Writer;)V

// 59: astore 6

// 61: aload 6

// 63: aload_1

// 64: invokevirtual 52 java/io/PrintWriter:println (Ljava/lang/String;)V

// 67: aload 6

// 69: ifnull +24 -> 93

// 72: aload 6

// 74: invokevirtual 55 java/io/PrintWriter:close ()V

// 77: goto +16 -> 93

// 80: astore_2

// 81: aload 6

// 83: ifnull +8 -> 91

// 86: aload 6

// 88: invokevirtual 55 java/io/PrintWriter:close ()V

// 91: aload_2

// 92: athrow

// 93: aload 5

// 95: ifnull +43 -> 138

// 98: aload 5

// 100: invokevirtual 58 java/io/BufferedWriter:close ()V

// 103: goto +35 -> 138

// 106: astore_3

// 107: aload_2

// 108: ifnonnull +8 -> 116

// 111: aload_3

// 112: astore_2

// 113: goto +13 -> 126

// 116: aload_2

// 117: aload_3

// 118: if_acmpeq +8 -> 126

// 121: aload_2

// 122: aload_3

// 123: invokevirtual 59 java/lang/Throwable:addSuppressed (Ljava/lang/Throwable;)V

// 126: aload 5

// 128: ifnull +8 -> 136

// 131: aload 5

// 133: invokevirtual 58 java/io/BufferedWriter:close ()V

// 136: aload_2

// 137: athrow

// 138: aload 4

// 140: ifnull +66 -> 206

// 143: aload 4

// 145: invokevirtual 65 java/io/FileWriter:close ()V

// 148: goto +58 -> 206

// 151: astore_3

// 152: aload_2

// 153: ifnonnull +8 -> 161

// 156: aload_3

// 157: astore_2

// 158: goto +13 -> 171

// 161: aload_2

// 162: aload_3

// 163: if_acmpeq +8 -> 171

// 166: aload_2

// 167: aload_3

// 168: invokevirtual 59 java/lang/Throwable:addSuppressed (Ljava/lang/Throwable;)V

// 171: aload 4

// 173: ifnull +8 -> 181

// 176: aload 4

// 178: invokevirtual 65 java/io/FileWriter:close ()V

// 181: aload_2

// 182: athrow

// 183: astore_3

// 184: aload_2

// 185: ifnonnull +8 -> 193

// 188: aload_3

// 189: astore_2

// 190: goto +13 -> 203

// 193: aload_2

// 194: aload_3

// 195: if_acmpeq +8 -> 203

// 198: aload_2

// 199: aload_3

// 200: invokevirtual 59 java/lang/Throwable:addSuppressed (Ljava/lang/Throwable;)V

// 203: aload_2

// 204: athrow

// 205: pop

// 206: return

// Line number table:

// Java source line #28 -> byte code offset #0

// Java source line #29 -> byte code offset #10

// Java source line #30 -> byte code offset #18

// Java source line #31 -> byte code offset #21

// Java source line #36 -> byte code offset #22

// Java source line #36 -> byte code offset #26

// Java source line #37 -> byte code offset #39

// Java source line #38 -> byte code offset #50

// Java source line #40 -> byte code offset #61

// Java source line #41 -> byte code offset #67

// Java source line #42 -> byte code offset #205

// Java source line #46 -> byte code offset #206

// Local variable table:

// start length slot name signature

// 0 207 0 this LoggingAspect

// 0 207 1 msg String

// 23 1 2 localObject1 Object

// 80 28 2 localObject2 Object

// 112 92 2 localObject3 Object

// 25 1 3 localObject4 Object

// 106 17 3 localThrowable1 Throwable

// 151 17 3 localThrowable2 Throwable

// 183 17 3 localThrowable3 Throwable

// 37 140 4 fw java.io.FileWriter

// 48 84 5 bw java.io.BufferedWriter

// 59 28 6 pw java.io.PrintWriter

// 21 1 12 localIOException1 java.io.IOException

// 205 1 13 localIOException2 java.io.IOException

// Exception table:

// from to target type

// 0 18 21 java/io/IOException

// 61 67 80 finally

// 50 93 106 finally

// 39 138 151 finally

// 26 183 183 finally

// 22 205 205 java/io/IOException

}

这真的让我难过,所以任何信息都会非常感激.谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/169280.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • LM算法代码_快速排序算法代码

    LM算法代码_快速排序算法代码LM算法+推导+C++代码实践一、算法推导二、代码实践参考一、算法推导二、代码实践#include<Eigen/Dense>#include<Eigen/Sparse>#include<iostream>#include<iomanip>#include<math.h>usingnamespacestd;usingnamespaceEigen;constdoubleDERIV_STEP=1

    2022年9月27日
    2
  • Java 数组转List的四种方式

    Java 数组转List的四种方式Java数组转List的四种方式第一种方式(未必最佳):使用ArrayList.asList(strArray)​ 使用Arrays工具类Arrays.asList(strArray)方式,转换完成后,只能对List数组进行查改,不能增删,增删就会抛出UnsupportedOperationException异常importjava.util.Arrays;importjava.util.List;publicstaticvoidDemo1(){String[

    2022年8月23日
    9
  • 最大子矩阵和 leetcode_leetcode有效的括号

    最大子矩阵和 leetcode_leetcode有效的括号给你一个二维矩阵 matrix 和一个整数 k ,矩阵大小为 m x n 由非负整数组成。矩阵中坐标 (a, b) 的 值 可由对所有满足 0 <= i <= a < m 且 0 <= j <= b < n 的元素 matrix[i][j](下标从 0 开始计数)执行异或运算得到。请你找出 matrix 的所有坐标中第 k 大的值(k 的值从 1 开始计数)。示例 1:输入:matrix = [[5,2],[1,6]], k = 1输出:7解释:坐标 (0,

    2022年8月11日
    5
  • VggNet10模型的cifar10深度学习训练

    VggNet10模型的cifar10深度学习训练目录一:数据准备:二:VGG模型三:代码部分1.input_data.py2.VGG.py3.tools.py4.train_and_val.py一:数据准备:先放些链接,cifar10的数据集的下载地址:http://www.cs.toronto.edu/~kriz/cifar.html用二进制tfcords的数据集训练,下载第三个,下载的数据文件集是…

    2022年8月30日
    2
  • string转map_map转bean对象

    string转map_map转bean对象前提:String为Json类型字符串maven<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.0</version></dependency>转换

    2025年10月24日
    2
  • 获取activexobject对象失败_在各种管理对象中最重要的是

    获取activexobject对象失败_在各种管理对象中最重要的是JavaScript中ActiveXObject对象是启用并返回Automation对象的引用

    2022年10月14日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号